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

com.fitbur.assertj.groups.Properties Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * 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.
 *
 * Copyright 2012-2016 the original author or authors.
 */
package com.fitbur.assertj.groups;

import static com.fitbur.assertj.util.Preconditions.checkNotNull;
import static com.fitbur.assertj.util.ArrayWrapperList.wrap;

import java.util.List;

import com.fitbur.assertj.util.VisibleForTesting;
import com.fitbur.assertj.util.introspection.IntrospectionError;
import com.fitbur.assertj.util.introspection.PropertySupport;



/**
 * Extracts the values of a specified property from the elements of a given {@link Iterable} or array.
 * 
 * @author Yvonne Wang
 * @author Mikhail Mazursky
 * @author Joel Costigliola
 * @author Florent Biville
 * @author Olivier Michallat
 */
public class Properties {

  @VisibleForTesting
  final String propertyName;
  final Class propertyType;

  @VisibleForTesting
  PropertySupport propertySupport = PropertySupport.instance();

  /**
   * Creates a new {@link Properties}.
   * @param propertyName the name of the property to be read from the elements of a {@code Iterable}. It may be a nested
   *          property (e.g. "address.street.number").
   * @param propertyType the type of property to extract
   * @throws NullPointerException if the given property name is {@code null}.
   * @throws IllegalArgumentException if the given property name is empty.
   * @return the created {@code Properties}.
   */
  public static  Properties extractProperty(String propertyName, Class propertyType) {
    checkIsNotNullOrEmpty(propertyName);
    return new Properties<>(propertyName, propertyType);
  }

  /**
   * Creates a new {@link Properties} with given propertyName and Object as property type..
   * @param propertyName the name of the property to be read from the elements of a {@code Iterable}. It may be a nested
   *          property (e.g. "address.street.number").
   * @throws NullPointerException if the given property name is {@code null}.
   * @throws IllegalArgumentException if the given property name is empty.
   * @return the created {@code Properties}.
   */
  public static Properties extractProperty(String propertyName) {
    return extractProperty(propertyName, Object.class);
  }
  
  private static void checkIsNotNullOrEmpty(String propertyName) {
    checkNotNull(propertyName, "The name of the property to read should not be null");
    if (propertyName.length() == 0) throw new IllegalArgumentException("The name of the property to read should not be empty");
  }

  @VisibleForTesting
  Properties(String propertyName, Class propertyType) {
    this.propertyName = propertyName;
    this.propertyType = propertyType;
  }

  /**
   * Specifies the target type of an instance that was previously created with {@link #extractProperty(String)}.
   * 

* This is so that you can write: *

 extractProperty("name").ofType(String.class).from(fellowshipOfTheRing);
* * instead of: *
 extractProperty("name", String.class).from(fellowshipOfTheRing);
*

* @param propertyType the type of property to extract. * @return a new {@code Properties} with the given type. */ public Properties ofType(Class propertyType) { return extractProperty(this.propertyName, propertyType); } /** * Extracts the values of the property (specified previously in {@link #extractProperty(String)}) from the elements * of the given {@link Iterable}. * @param c the given {@code Iterable}. * @return the values of the previously specified property extracted from the given {@code Iterable}. * @throws IntrospectionError if an element in the given {@code Iterable} does not have a property with a matching name. */ public List from(Iterable c) { return propertySupport.propertyValues(propertyName, propertyType, c); } /** * Extracts the values of the property (specified previously in {@link #extractProperty(String)}) from the elements * of the given array. * @param array the given array. * @return the values of the previously specified property extracted from the given array. * @throws IntrospectionError if an element in the given array does not have a property with a matching name. */ public List from(Object[] array) { return propertySupport.propertyValues(propertyName, propertyType, wrap(array)); } }