Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.web.bindery.autobean.shared.impl;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;
import com.google.web.bindery.autobean.shared.AutoBeanVisitor;
import com.google.web.bindery.autobean.shared.AutoBeanVisitor.ParameterizationVisitor;
import com.google.web.bindery.autobean.shared.Splittable;
import com.google.web.bindery.autobean.shared.ValueCodex;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
/**
* Contains the implementation details of AutoBeanCodex. This type was factored
* out of AutoBeanCodex so that various implementation details can be accessed
* without polluting a public API.
*/
public class AutoBeanCodexImpl {
/**
* Describes a means of encoding or decoding a particular type of data to or
* from a wire format representation. Any given instance of a Coder should be
* stateless; any state required for operation must be maintained in an
* {@link EncodeState}.
*/
public interface Coder {
Object decode(EncodeState state, Splittable data);
void encode(EncodeState state, Object value);
Splittable extractSplittable(EncodeState state, Object value);
}
/**
* Contains transient state for Coder operation.
*/
public static class EncodeState {
/**
* Constructs a state object used for decoding payloads.
*/
public static EncodeState forDecode(AutoBeanFactory factory) {
return new EncodeState(factory, null);
}
/**
* Constructs a state object used for encoding payloads.
*/
public static EncodeState forEncode(AutoBeanFactory factory, StringBuilder sb) {
return new EncodeState(factory, sb);
}
/**
* Constructs a "stateless" state for testing Coders that do not require
* AutoBean implementation details.
*/
public static EncodeState forTesting() {
return new EncodeState(null, null);
}
final EnumMap enumMap;
final AutoBeanFactory factory;
final StringBuilder sb;
final Stack> seen;
private EncodeState(AutoBeanFactory factory, StringBuilder sb) {
this.factory = factory;
enumMap = factory instanceof EnumMap ? (EnumMap) factory : null;
this.sb = sb;
this.seen = sb == null ? null : new Stack>();
}
}
/**
* Dynamically creates a Coder that is capable of operating on a particular
* parameterization of a datastructure (e.g. {@code Map>}
* ).
*/
static class CoderCreator extends ParameterizationVisitor {
private Stack stack = new Stack();
@Override
public void endVisitType(Class> type) {
if (List.class.equals(type) || Set.class.equals(type)) {
stack.push(collectionCoder(type, stack.pop()));
} else if (Map.class.equals(type)) {
// Note that the parameters are passed in reverse order
stack.push(mapCoder(stack.pop(), stack.pop()));
} else if (Splittable.class.equals(type)) {
stack.push(splittableCoder());
} else if (type.getEnumConstants() != null) {
@SuppressWarnings(value = {"unchecked"})
Class> enumType = (Class>) type;
stack.push(enumCoder(enumType));
} else if (ValueCodex.canDecode(type)) {
stack.push(valueCoder(type));
} else {
stack.push(objectCoder(type));
}
}
public Coder getCoder() {
assert stack.size() == 1 : "Incorrect size: " + stack.size();
return stack.pop();
}
}
/**
* Constructs one of the lightweight collection types.
*/
static class CollectionCoder implements Coder {
private final Coder elementDecoder;
private final Class> type;
public CollectionCoder(Class> type, Coder elementDecoder) {
this.elementDecoder = elementDecoder;
this.type = type;
}
public Object decode(EncodeState state, Splittable data) {
Collection