Skip to content

Instantly share code, notes, and snippets.

@ripper234
Created November 28, 2011 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ripper234/1399990 to your computer and use it in GitHub Desktop.
Save ripper234/1399990 to your computer and use it in GitHub Desktop.
An enhancement of ObjectType that supports filtering
- com.google.guava -> guava 10.0.1
package controllers;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import play.db.jpa.Model;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
public class FilteredObjectType extends CRUD.ObjectType{
private final Collection<ObjectField> filteredFields;
public FilteredObjectType(Class<? extends Model> modelClass, String ... filteredFieldNames) {
this(new CRUD.ObjectType(modelClass), filteredFieldNames);
}
public FilteredObjectType(CRUD.ObjectType objectType, final String ... filteredFieldNames) {
super(objectType.entityClass);
final HashSet<String> filteredFieldNamesSet = new HashSet<String>(Arrays.asList(filteredFieldNames));
filteredFields = Collections2.filter(super.getFields(), new Predicate<ObjectField>() {
public boolean apply(ObjectField objectField) {
return !filteredFieldNamesSet.contains(objectField.name);
}
});
}
@Override
public List<ObjectField> getFields() {
// defensive copy
return newArrayList(filteredFields);
}
}
public static void show(long id) {
MyModel object = MyModel.findById(id);
CRUD.ObjectType type = new FilteredObjectType(MyModel.class, "filteredField1", "filteredField2");
render(type, object);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment