Class IfDefault

java.lang.Object
com.googlecode.objectify.condition.ValueIf<Object>
com.googlecode.objectify.condition.IfDefault
All Implemented Interfaces:
If<Object,​Object>, InitializeIf

public class IfDefault
extends ValueIf<Object>
implements InitializeIf

This condition tests against the default value of the field that it is placed upon, whatever that default may be. If you initialize the field with a value, this condition will use that value as the comparison. For example, if you have a class like this:

 public class MyEntity {
     @Id Long id;
     @IgnoreSave(IfDefault.class) String foo = "defaultFoo";
 }
 

The foo field will be left unsaved when it has the value "defaultFoo".

Specifically, this conditional constructs an instance of the class in which the field is declared and stores the default field value for later comparison. Note that if you initialize the field in your default constructor, this counts!

There is one important caveat: Objectify treats each declared class in a type hierarchy separately. The class in which the field is declared have a no-arg constructor, and it alone is used to determine the default value. This will NOT work:

 public class MyBase {
     @Id Long id;
     @IgnoreSave(IfDefault.class) String foo = "baseFoo";
 }
 public class MyEntity extends MyBase {
     public MyEntity() {
         foo = "subclassFoo";
     }
 }
 

In this example, the default value will be "baseFoo".

Author:
Jeff Schnitzer
  • Constructor Details

    • IfDefault

      public IfDefault()
  • Method Details

    • init

      public void init(ObjectifyFactory fact, Field field)
      Description copied from interface: InitializeIf
      Instructs the condition instance which field it lives on.
      Specified by:
      init in interface InitializeIf
      Parameters:
      fact - is just handy to have around
      field - is the field which has the annotation with this condition.
    • matchesValue

      public boolean matchesValue(Object value)
      Description copied from interface: If
      Test a simple property value.
      Specified by:
      matchesValue in interface If<Object,​Object>
      Parameters:
      value - is the actual value of a particular field
      Returns:
      true if the value matches the condition defined by an instance of this interface.