package foam.core;

import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
import java.util.Map;

public abstract class AbstractFObject
  extends ContextAwareSupport
  implements FObject
{

  public static FObject maybeClone(FObject fo) {
    return ( fo == null ? null : fo.fclone() );
  }

  public FObject fclone() {
    try {
      FObject ret = (FObject) getClassInfo().newInstance();
      List<PropertyInfo> props = getClassInfo().getAxiomsByClass(PropertyInfo.class);
      for( PropertyInfo pi : props ) {
        pi.set(ret, pi.get(this));
      }
      return ret;
    } catch (IllegalAccessException | InstantiationException e) {
      return null;
    }
  }

  public Map diff(FObject obj) {
    List props = getClassInfo().getAxiomsByClass(PropertyInfo.class);
    Iterator i = props.iterator();

    Map result = new HashMap();
    while ( i.hasNext() ) {
      PropertyInfo prop = (PropertyInfo) i.next();
      prop.diff(this, obj, result, prop);
    }

    return result;
  }

  public int compareTo(Object o) {
    if ( o == this ) return 0;
    if ( o == null ) return 1;
    if ( ! ( o instanceof FObject ) ) return 1;

    if ( getClass() != o.getClass() ) {
      return getClassInfo().getId().compareTo(((FObject)o).getClassInfo().getId());
    }

    List props = getClassInfo().getAxiomsByClass(PropertyInfo.class);
    Iterator i = props.iterator();

    int result;
    while ( i.hasNext() ) {
      result = ((PropertyInfo)i.next()).compare(this, o);
      if ( result != 0 ) return result;
    }

    return 0;
  }

  public FObject setProperty(String prop, Object value) {
    PropertyInfo property = ((PropertyInfo) getClassInfo().getAxiomByName(prop));
    property.set(this, value);
    return this;
  }

  public Object getProperty(String prop) {
    return ((PropertyInfo) getClassInfo().getAxiomByName(prop)).get(this);
  }
}

@@ -1696,24 +1696,77 @@
 ame(prop));%0A
+    if (property == null) %7B%0A      return null;%0A    %7D%0A
     property
@@ -1853,22 +1853,39 @@
 ) %7B%0A    
-return
+PropertyInfo property =
  ((Prope
@@ -1929,16 +1929,90 @@
 e(prop))
+;%0A    if (property == null) %7B%0A      return null;%0A    %7D%0A    return property
 .get(thi

add checks for null