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

com.rapiddweller.common.NameUtil Maven / Gradle / Ivy

Go to download

'rapiddweller Common' is an open source Java library forked from Databene Commons by Volker Bergmann. It provides extensions to the Java core library by utility classes, abstract concepts and concrete implementations.

There is a newer version: 2.0.1-jdk-11
Show newest version
/*
 * 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 com.rapiddweller.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Provides convenience methods for {@link Named} objects.
 * Created: 12.08.2010 09:21:46
 *
 * @author Volker Bergmann
 * @since 0.5.4
 */
public class NameUtil {

  private NameUtil() {
  }

  /**
   * Get names string [ ].
   *
   * @param objects the objects
   * @return the string [ ]
   */
  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;
  }

  /**
   * Gets names.
   *
   * @param      the type parameter
   * @param objects the objects
   * @return the names
   */
  public static > List getNames(T objects) {
    List result = new ArrayList<>(objects.size());
    for (Named object : objects) {
      result.add(object.getName());
    }
    return result;
  }

  /**
   * Get names as array string [ ].
   *
   * @param      the type parameter
   * @param objects the objects
   * @return the string [ ]
   */
  public static > String[] getNamesAsArray(T objects) {
    String[] result = new String[objects.size()];
    int i = 0;
    for (Named object : objects) {
      result[i++] = object.getName();
    }
    return result;
  }

  /**
   * Order by name.
   *
   * @param      the type parameter
   * @param objects the objects
   */
  public static  void orderByName(T[] objects) {
    Arrays.sort(objects, new NameComparator());
  }

  /**
   * Order by name.
   *
   * @param      the type parameter
   * @param objects the objects
   */
  public static  void orderByName(List objects) {
    objects.sort(new NameComparator());
  }

  /**
   * Index of int.
   *
   * @param name    the name
   * @param objects the objects
   * @return the int
   */
  public static int indexOf(String name, List objects) {
    for (int i = 0; i < objects.size(); i++) {
      if (name.equals(objects.get(i).getName())) {
        return i;
      }
    }
    return -1;
  }

  /**
   * Index of int.
   *
   * @param name    the name
   * @param objects the objects
   * @return the int
   */
  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;
  }

  /**
   * Sort.
   *
   * @param namedObjects the named objects
   */
  public static void sort(List namedObjects) {
    namedObjects.sort(new NameComparator());
  }

  /**
   * Find list.
   *
   * @param     the type parameter
   * @param list   the list
   * @param filter the filter
   * @return the list
   */
  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;
  }

  /**
   * Find by name t.
   *
   * @param    the type parameter
   * @param name  the name
   * @param array the array
   * @return the t
   */
  public static  T findByName(String name, T[] array) {
    for (T item : array) {
      if (NullSafeComparator.equals(item.getName(), name)) {
        return item;
      }
    }
    return null;
  }

  /**
   * Find by name t.
   *
   * @param   the type parameter
   * @param name the name
   * @param list the list
   * @return the t
   */
  public static  T findByName(String name, List list) {
    for (T item : list) {
      if (NullSafeComparator.equals(item.getName(), name)) {
        return item;
      }
    }
    return null;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy