org.databene.commons.NameUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of databene-commons Show documentation
Show all versions of databene-commons Show documentation
'databene commons' is an open source Java library by Volker Bergmann.
It provides extensions to the Java core library by utility classes, abstract concepts
and concrete implementations.
/*
* Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
* All rights reserved.
*
* 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.databene.commons;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Provides convenience methods for {@link Named} objects.
* Created: 12.08.2010 09:21:46
* @since 0.5.4
* @author Volker Bergmann
*/
public class NameUtil {
private NameUtil() { }
public static String[] getNames(Named[] objects) {
String[] result = new String[objects.length];
for (int i = 0; i < objects.length; i++)
result[i] = objects[i].getName();
return result;
}
public static > List getNames(T objects) {
List result = new ArrayList(objects.size());
for (Named object : objects)
result.add(object.getName());
return result;
}
public static void orderByName(T[] objects) {
Arrays.sort(objects, new NameComparator());
}
public static void orderByName(List objects) {
Collections.sort(objects, new NameComparator());
}
public static int indexOf(String name, List extends Named> objects) {
for (int i = 0; i < objects.size(); i++)
if (name.equals(objects.get(i).getName()))
return i;
return -1;
}
public static int indexOf(String name, Named[] objects) {
for (int i = 0; i < objects.length; i++)
if (name.equals(objects[i].getName()))
return i;
return -1;
}
public static void sort(List extends Named> namedObjects) {
Collections.sort(namedObjects, new NameComparator());
}
public static List find(List list, Filter filter) {
List result = new ArrayList();
for (T object : list)
if (filter.accept(object.getName()))
result.add(object);
return result;
}
public static T findByName(String name, T[] array) {
for (T item : array)
if (NullSafeComparator.equals(item.getName(), name))
return item;
return null;
}
}