001    /*
002     * Created on Feb 22, 2011
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2011 the original author or authors.
014     */
015    package org.fest.assertions.groups;
016    
017    import static org.fest.assertions.util.ArrayWrapperList.wrap;
018    
019    import java.util.Collection;
020    import java.util.List;
021    
022    import org.fest.assertions.internal.PropertySupport;
023    import org.fest.util.IntrospectionError;
024    import org.fest.util.VisibleForTesting;
025    
026    /**
027     * Extracts the values of a specified property from the elements of a given <code>{@link Collection}</code> or array.
028     *
029     * @author Yvonne Wang
030     */
031    public class Properties {
032    
033      @VisibleForTesting final String propertyName;
034    
035      @VisibleForTesting PropertySupport propertySupport = PropertySupport.instance();
036    
037      /**
038       * Creates a new <code>{@link Properties}</code>.
039       * @param propertyName the name of the property to be read from the elements of a {@code Collection}. It may be a
040       * nested property (e.g. "address.street.number").
041       * @throws NullPointerException if the given property name is {@code null}.
042       * @throws IllegalArgumentException if the given property name is empty.
043       * @return the created {@code Properties}.
044       */
045      public static Properties extractProperty(String propertyName) {
046        checkIsNotNullOrEmpty(propertyName);
047        return new Properties(propertyName);
048      }
049    
050      private static void checkIsNotNullOrEmpty(String propertyName) {
051        if (propertyName == null) throw new NullPointerException("The name of the property to read should not be null");
052        if (propertyName.length() == 0)
053          throw new IllegalArgumentException("The name of the property to read should not be empty");
054      }
055    
056      @VisibleForTesting Properties(String propertyName) {
057        this.propertyName = propertyName;
058      }
059    
060      /**
061       * Extracts the values of the property (specified previously in <code>{@link #extractProperty(String)}</code>) from
062       * the elements of the given <code>{@link Collection}</code>.
063       * @param c the given {@code Collection}.
064       * @return the values of the previously specified property extracted from the given {@code Collection}.
065       * @throws IntrospectionError if an element in the given {@code Collection} does not have a property with a matching
066       * name.
067       */
068      public List<?> from(Collection<?> c) {
069        return propertySupport.propertyValues(propertyName, c);
070      }
071    
072      /**
073       * Extracts the values of the property (specified previously in <code>{@link #extractProperty(String)}</code>) from
074       * the elements of the given array.
075       * @param array the given array.
076       * @return the values of the previously specified property extracted from the given array.
077       * @throws IntrospectionError if an element in the given array does not have a property with a matching name.
078       */
079      public List<?> from(Object[] array) {
080        return propertySupport.propertyValues(propertyName, wrap(array));
081      }
082    }