public void encode(EncodeState state, Object value) { if (value == null) { state.sb.append("null"); return; } state.sb.append(((Splittable) value).getPayload()); }
if (value.isString()) { if (!otherValue.isString()) { return false; return value.asString().equals(otherValue.asString()); if (value.isIndexed()) { if (!otherValue.isIndexed()) { return false; if (value.size() != otherValue.size()) { return false; for (int i = 0, j = value.size(); i < j; i++) { if (!sameOrEquals(value.get(i), otherValue.get(i), pending)) { return false; if (value.isKeyed()) { if (!otherValue.isKeyed()) { return false; List<String> keys = value.getPropertyKeys(); for (String key : keys) { if (value.isNull(key)) { if (!otherValue.isNull(key)) { return false; } else if (otherValue.isNull(key)
@Override public Boolean decode(Class<?> clazz, Splittable value) { return value.asBoolean(); }
public void clear() { for (String key : data.getPropertyKeys()) { Splittable.NULL.assign(data, key); reified.setReified(key, null); } }
static <Q> Q reify(EncodeState state, Splittable data, int index, Coder coder) { if (data.isNull(index)) { return null; } @SuppressWarnings("unchecked") Q toReturn = (Q) coder.decode(state, data.get(index)); data.setReified(String.valueOf(index), toReturn); return toReturn; }
@Override public E remove(int index) { E toReturn = get(index); // XXX This is terrible, use Array.splice int newSize = data.size() - 1; for (int i = index; i < newSize; i++) { data.get(i + 1).assign(data, i); // reified value for index 'i' may needs to be updated if (data.isReified(String.valueOf(i + 1))) { data.setReified(String.valueOf(i), data.getReified(String.valueOf(i + 1))); } else if (data.isReified(String.valueOf(i))) { // Remove outdated value. Will be regenerated if needed. data.removeReified(String.valueOf(i)); } } // clean-up last entry as one element has been removed data.removeReified(String.valueOf(newSize)); data.setSize(newSize); return toReturn; }
private V getRaw(String encodedKey) { if (reified.isReified(encodedKey)) { @SuppressWarnings("unchecked") V toReturn = (V) reified.getReified(encodedKey); return toReturn; } // Both undefined or an explicit null should return null here if (data.isNull(encodedKey)) { return null; } Splittable value = data.get(encodedKey); @SuppressWarnings("unchecked") V toReturn = (V) valueCoder.decode(state, value); reified.setReified(encodedKey, toReturn); return toReturn; } }
if (ValueCodex.canDecode(keyType) || !split.isIndexed()) { List<String> keys = split.getPropertyKeys(); for (String propertyKey : keys) { Object key = (keyType == String.class) ? propertyKey : ValueCodex.decode(keyType, StringQuoter.split(propertyKey)); if (split.isNull(propertyKey)) { map.put(key, null); } else { Splittable valueSplit = split.get(propertyKey); Object value = null; if (ValueCodex.canDecode(valueType)) { if (split.size() != 2) { throw new UnsupportedOperationException(); List<?> keys = (List<?>) decode(source, List.class, keyType, split.get(0)); List<?> values = (List<?>) decode(source, List.class, valueType, split.get(1)); if (keys.size() != values.size()) { throw new UnsupportedOperationException();
static void set(EncodeState state, Splittable data, int index, Coder coder, Object value) { data.setReified(String.valueOf(index), value); if (value == null) { Splittable.NULL.assign(data, index); return; } Splittable backing = coder.extractSplittable(state, value); if (backing == null) { /* * External data type, such as an ArrayList or a concrete implementation * of a setter's interface type. This means that a slow serialization pass * is necessary. */ data.setReified(AbstractAutoBean.UNSPLITTABLE_VALUES_KEY, true); } else { backing.assign(data, index); } }
} else if (object instanceof Splittable) { Splittable split = (Splittable) object; if (split.isString()) return encodeValue(split.asString()); List<String> keys = split.getPropertyKeys(); for (String key : keys) { sb.append(encodeValue(key)); sb.append(" : "); sb.append(encodeValue(split.get(key))); if (c < keys.size() - 1) sb.append(", ");
public void processPayload(Receiver<Void> receiver, String payload) { Splittable raw = StringQuoter.split(payload); @SuppressWarnings("unchecked") Receiver<Object> callback = (Receiver<Object>) state.invocations.get(0).getReceiver(); if (!raw.isNull("error")) { Splittable error = raw.get("error"); ServerFailure failure = new ServerFailure(error.get("message").asString(), error.get("code").asString(), payload, true); fail(receiver, failure); return; } Splittable result = raw.get("result"); @SuppressWarnings("unchecked") Class<BaseProxy> target = (Class<BaseProxy>) state.invocations.get(0).getRequestData().getReturnType(); SimpleProxyId<BaseProxy> id = getRequestFactory().allocateId(target); AutoBean<BaseProxy> bean = createProxy(target, id, true); // XXX expose this as a proper API ((AbstractAutoBean<?>) bean).setData(result); // AutoBeanCodex.decodeInto(result, bean); if (callback != null) { callback.onSuccess(bean.as()); } if (receiver != null) { receiver.onSuccess(null); } }
for (int i = 0, j = split.size(); i < j; i++) { if (split.isNull(i)) { collection.add(null); } else { Object element = ValueCodex.decode(elementType, split.get(i)); collection.add(element); for (int i = 0, j = split.size(); i < j; i++) { if (split.isNull(i)) { collection.add(null); } else { Object element = decode(source, elementType, null, split.get(i)); collection.add(element);
public Object decode(EncodeState state, Splittable data) { Map<Object, Object> toReturn; if (data.isIndexed()) { assert data.size() == 2 : "Wrong data size: " + data.size(); toReturn = new SplittableComplexMap<Object, Object>(data, keyDecoder, valueDecoder, state); } else { toReturn = new SplittableSimpleMap<Object, Object>(data, keyDecoder, valueDecoder, state); } return toReturn; }
@Override public E get(int index) { if (data.isReified(String.valueOf(index))) { @SuppressWarnings("unchecked") E toReturn = (E) data.getReified(String.valueOf(index)); return toReturn; } // javac generics bug return SplittableList.<E> reify(state, data, index, elementCoder); }
public Collection<V> values() { return new AbstractCollection<V>() { final List<String> keys = data.getPropertyKeys(); @Override public Iterator<V> iterator() { return new Iterator<V>() { final Iterator<String> it = keys.iterator(); String lastEncodedKey; public boolean hasNext() { return it.hasNext(); } public V next() { lastEncodedKey = it.next(); return getRaw(lastEncodedKey); } public void remove() { Splittable.NULL.assign(data, lastEncodedKey); reified.setReified(lastEncodedKey, null); } }; } @Override public int size() { return keys.size(); } }; }
@Override public String decode(Class<?> clazz, Splittable value) { return value.asString(); }
public static <T> AutoBean<T> doDecode(EncodeState state, Class<T> clazz, Splittable data) { /* * If we decode the same Splittable twice, re-use the ProxyAutoBean to * maintain referential integrity. If we didn't do this, either facade would * update the same backing data, yet not be the same object via == * comparison. */ @SuppressWarnings("unchecked") AutoBean<T> toReturn = (AutoBean<T>) data.getReified(AutoBeanCodexImpl.class.getName()); if (toReturn != null) { return toReturn; } toReturn = state.factory.create(clazz); data.setReified(AutoBeanCodexImpl.class.getName(), toReturn); if (toReturn == null) { throw new IllegalArgumentException(clazz.getName()); } ((AbstractAutoBean<T>) toReturn).setData(data); return toReturn; }
@Override public Short decode(Class<?> clazz, Splittable value) { return (short) value.asNumber(); }