EnumeratedMultiProperty.java
001 /**
002  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003  */
004 package net.sourceforge.pmd.lang.rule.properties;
005 
006 import net.sourceforge.pmd.util.StringUtil;
007 
008 /**
009  * Defines a datatype with a set of preset values of any type as held within a pair of
010  * maps. While the values are not serialized out, the labels are and serve as keys to
011  * obtain the values.  The choices() method provides the ordered selections to be used
012  * in an editor widget.
013  
014  @author Brian Remedios
015  */
016 public class EnumeratedMultiProperty<E> extends AbstractEnumeratedProperty<E, Object[]{
017   
018   /**
019    * Constructor for EnumeratedProperty.
020    @param theName String
021    @param theDescription String
022      @param theLabels String[]
023      @param theChoices E[]
024      @param choiceIndices int[]
025    @param theUIOrder float
026    @throws IllegalArgumentException
027    */
028   public EnumeratedMultiProperty(String theName, String theDescription, String[] theLabels, E[] theChoices, int[] choiceIndices, float theUIOrder) {
029     super(theName, theDescription, theLabels, theChoices, choiceIndices, theUIOrder, true);
030   }
031   
032   /**
033    @return Class
034    @see net.sourceforge.pmd.PropertyDescriptor#type()
035    */
036   public Class<Object[]> type() {
037     return Object[].class;
038   }
039   
040   /**
041    @return boolean
042    @see net.sourceforge.pmd.PropertyDescriptor#isMultiValue()
043    */
044   @Override
045   public boolean isMultiValue() {
046     return true;
047   }
048   
049   /**
050    @param value Object
051    @return String
052    @see net.sourceforge.pmd.PropertyDescriptor#errorFor(Object)
053    */
054   @Override
055   public String errorFor(Object value) {
056     Object[] values = (Object[])value;
057     for (int i=0; i<values.length; i++) {
058       if (!labelsByChoice.containsKey(values[i])) {
059         return nonLegalValueMsgFor(values[i]);
060       }
061     }
062     return null;
063   }
064   
065   /**
066    
067    @param value String
068    @return Object
069    @throws IllegalArgumentException
070    @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
071    */
072   public Object[] valueFrom(String valuethrows IllegalArgumentException {
073     String[] strValues = StringUtil.substringsOf(value, multiValueDelimiter());
074     
075     Object[] values = new Object[strValues.length];
076     for (int i=0;i<values.length; i++) {
077         values[i= choiceFrom(strValues[i]);
078     }
079     return values;
080   }
081   
082   /**
083    
084    @param value Object
085    @return String
086    @see net.sourceforge.pmd.PropertyDescriptor#asDelimitedString(Object)
087    */
088   @Override
089   public String asDelimitedString(Object[] value) {
090     Object[] choices = value;
091     
092     StringBuilder sb = new StringBuilder();
093 
094     sb.append(labelsByChoice.get(choices[0]));
095     
096     for (int i=1; i<choices.length; i++) {
097       sb.append(multiValueDelimiter());
098       sb.append(labelsByChoice.get(choices[i]));
099     }
100 
101     return sb.toString();
102   }
103 }