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.*;
020    
021    import org.fest.assertions.internal.PropertySupport;
022    import org.fest.util.*;
023    
024    /**
025     * Extracts the values of an specified property from the elements of a given <code>{@link Collection}</code> or array.
026     *
027     * @author Yvonne Wang
028     */
029    public class Properties {
030    
031      @VisibleForTesting final String propertyName;
032    
033      @VisibleForTesting PropertySupport propertySupport = PropertySupport.instance();
034    
035      /**
036       * Creates a new <code>{@link Properties}</code>.
037       * @param propertyName the name of the property to be read from the elements of a {@code Collection}. It may be a
038       * nested property (e.g. "address.street.number").
039       * @throws NullPointerException if the given property name is {@code null}.
040       * @throws IllegalArgumentException if the given property name is empty.
041       * @return the created {@code Properties}.
042       */
043      public static Properties extractProperty(String propertyName) {
044        checkIsNotNullOrEmpty(propertyName);
045        return new Properties(propertyName);
046      }
047    
048      private static void checkIsNotNullOrEmpty(String propertyName) {
049        if (propertyName == null) throw new NullPointerException("The name of the property to read should not be null");
050        if (propertyName.length() == 0)
051          throw new IllegalArgumentException("The name of the property to read should not be empty");
052      }
053    
054      @VisibleForTesting Properties(String propertyName) {
055        this.propertyName = propertyName;
056      }
057    
058      /**
059       * Extracts the values of the property (specified previously in <code>{@link #extractProperty(String)}</code>) from
060       * the elements of the given <code>{@link Collection}</code>.
061       * @param c the given {@code Collection}.
062       * @return the values of the previously specified property extracted from the given {@code Collection}.
063       * @throws IntrospectionError if an element in the given {@code Collection} does not have a property with a matching
064       * name.
065       */
066      public List<?> from(Collection<?> c) {
067        return propertySupport.propertyValues(propertyName, c);
068      }
069    
070      /**
071       * Extracts the values of the property (specified previously in <code>{@link #extractProperty(String)}</code>) from
072       * the elements of the given array.
073       * @param array the given array.
074       * @return the values of the previously specified property extracted from the given array.
075       * @throws IntrospectionError if an element in the given array does not have a property with a matching name.
076       */
077      public List<?> from(Object[] array) {
078        return propertySupport.propertyValues(propertyName, wrap(array));
079      }
080    }