All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.objectify.condition.IfDefault Maven / Gradle / Ivy

Go to download

*** THIS VERSION UPLOADED FOR USE WITH CEDAR-COMMON, TO AVOID DEPENDENCIES ON GOOGLE CODE-BASED MAVEN REPOSITORIES. *** The simplest convenient interface to the Google App Engine datastore

The newest version!
package com.googlecode.objectify.condition;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

import com.googlecode.objectify.impl.TypeUtils;


/**
 * 

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;
 *     @NotSaved(IfDefault.class) String foo = "defaultFoo";
 * }
 * 
* *

The {@code foo} field will be left unsaved when it has the value "defaultFoo".

* *

Specifically, this conditional constructs an instance of your entity class * using the default (no-arg) constructor and stores the default field value for * later comparison. Note that if you initialize the field in your default constructor, * this counts!

* * @author Jeff Schnitzer */ public class IfDefault extends ValueIf { Object defaultValue; public IfDefault(Class clazz, Field field) { Constructor ctor = TypeUtils.getNoArgConstructor(clazz); Object pojo = TypeUtils.newInstance(ctor); this.defaultValue = TypeUtils.field_get(field, pojo); } @Override public boolean matches(Object value) { if (this.defaultValue == null) return value == null; else return this.defaultValue.equals(value); } }