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

org.ujoframework.criterion.CriteriaTool Maven / Gradle / Ivy

There is a newer version: 1.22
Show newest version
/*
 *  Copyright 2007-2010 Pavel Ponec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */   
   

package org.ujoframework.criterion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.ujoframework.Ujo;
import org.ujoframework.core.UjoComparator;


/**
 * The Criteria class is a simple tool to search UJO objects in the list.
 * This class takes full advantage of architecture UJO objects. See the next sample.
 * 
 * Person child  = new Person("Pavel", 140.0);
 * Person mother = new Person("Mary", 150.0);
 * Person father = new Person("John", 160.0);
 *
 * child.set(MOTHER, mother);
 * child.set(FATHER, father);
 *
 * List<Person> persons = Arrays.asList(child, mother, father);
 *
 * Criterion<Person> exp = Criterion.newInstance(NAME, "John");
 * UjoComparator<Person> sort = UjoComparator.create(HIGH, NAME);
 * List<Person> result = CriteriaTool.newInstance().select(persons, exp, sort);
* * @author Pavel Ponec * @since 0.90 */ public class CriteriaTool { /** Find the first UJO by an criterion or return NULL if any object was not found. */ public UJO findFirst(List list, Criterion criterion) { for (UJO ujo : list) { if (criterion.evaluate(ujo)) { return ujo; } } return null; } /** Create a copy of the list and sort it. */ public List select(List list, UjoComparator comparator) { List result = new ArrayList(list); Collections.sort(result, comparator); return result; } /** Create a sublist of a list by an Ujo criterion. */ public List select(List list, Criterion criterion) { return select(list, criterion, null); } /** Create a sublist of a list by an Ujo criterion. */ public List select(List list, Criterion criterion, UjoComparator sorting) { List result = new ArrayList(); for (UJO ujo : list) { if (criterion.evaluate(ujo)) { result.add(ujo); } } if (sorting != null) { Collections.sort(result, sorting); } return result; } // ----------- STATIC ------------- /** Create a new instance */ public static CriteriaTool newInstance() { return new CriteriaTool(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy