Immutable mu = new Immutable(); mu.state[1] = 'o';
private static Immutable immutable = new Immutable(0); public static void main(String[] args) { IntStream.range(0,10).forEach(i -> { immutable = immutable.changeState(i); System.out.println(immutable.toString()); }); }
private static Immutable immutable = new Immutable(0); public static void main(String[] args) { IntStream.range(0,10).forEach(i -> { Immutable localImmutable = immutable.changeState(i); System.out.println(localImmutable.toString()); }); }
new Immutable(one, fish, two, fish, red, fish, blue, fish); /*1 */ params = new ImmutableParameters(); /*2 */ params.setType("fowl"); new Immutable(params); factory = new ImmutableFactory(); /*3 */ factory.setType("fish"); factory.getInstance(); Immutable boringImmutable = new Immutable(); /* 4 */ Immutable lessBoring = boringImmutable.setType("vegetable");
public Object completeRoot(Object root, UnmarshallingContext navigator, String namespaceURI, String localName) { if(log.isTraceEnabled()) { log.trace("completeRoot root=" + root + " navigator=" + navigator + " namespaceURI=" + namespaceURI + " localName=" + localName ); } if(root instanceof Immutable) { root = ((Immutable)root).newInstance(); } return root; }
private static Object get(Object o, String localName, FieldInfo fieldInfo) { if(log.isTraceEnabled()) { log.trace("get object=" + o + " localName=" + localName); } Object value; if(o instanceof Immutable) { Immutable con = ((Immutable)o); value = con.getChild(localName); } else { value = fieldInfo.getValue(o); } return value; }
private static void set(Object parent, Object child, String localName, FieldInfo fieldInfo) { if(log.isTraceEnabled()) { log.trace("set parent=" + parent + " child=" + child + " localName=" + localName); } if(fieldInfo.isWritable()) { fieldInfo.setValue(parent, child); } else if(parent instanceof Immutable) { ((Immutable)parent).addChild(localName, child); } else { throw new IllegalStateException("Neither write method nor field were found for " + fieldInfo.getName() + " and the parent object is not an immutable container: parent=" + parent.getClass() + ", localName=" + localName + ", parent=" + parent + ", child=" + child ); } }
public void addChild(Object parent, Object child, UnmarshallingContext ctx, String namespaceURI, String localName) { if(log.isTraceEnabled()) { log.trace("addChild parent=" + parent + " child=" + child + " namespaceURI=" + namespaceURI + " localName=" + localName ); } if(child instanceof Immutable) { child = ((Immutable)child).newInstance(); } setChild(child, parent, localName); }
public class Sharable { private Sharable() { } public static Immutable getImmutableInstance(int a, int b) { return new Immutable(a, b); } }
Map<String, Immutable> _vars = new HashMap<String, Immutable>(); public void modify(String key, int v) { _vars.put(key, new Immutable(v); }
Immutable oldObj = new Immutable(...); Immutable newObj = new Immutable(oldObj.property1, "a new value for property 2");
private static Object newInstance(Class cls) { if(log.isTraceEnabled()) { log.trace("new " + cls.getName()); } Object instance; try { Constructor ctor = cls.getConstructor(null); instance = ctor.newInstance(null); } catch(NoSuchMethodException e) { log.warn("No no-arg constructor in " + cls); instance = new Immutable(cls); } catch(Exception e) { throw new IllegalStateException("Failed to create an instance of " + cls + " with the no-arg constructor: " + e.getMessage() ); } return instance; }