1 package org.jscsi.target.settings; 2 3 4 import org.jscsi.target.settings.entry.Entry; 5 6 7 /** 8 * {@link KeySet} objects are used by {@link Entry} instances for storing all <i>keys</i> that might be used to 9 * negotiate the parameter managed by the {@link Entry}. Individual <i>keys</i> are represented as {@link String} 10 * objects. 11 * <p> 12 * Although most parameters are associated with just one <i>key</i>, there are some exceptions, hence justifying the 13 * added complexity of using a dedicated class for managing <i>keys</i> in favor of simply using multiple {@link String} 14 * objects. 15 * 16 * @author Andreas Ergenzinger 17 */ 18 public final class KeySet { 19 20 /** 21 * An array containing all <i>keys</i> which are part of the set. 22 */ 23 final String[] values; 24 25 /** 26 * Constructor for creating {@link KeySet} instances with just one <i>key</i>. 27 * 28 * @param value the <i>key</i> 29 */ 30 public KeySet (final String value) { 31 values = new String[1]; 32 values[0] = value; 33 } 34 35 /** 36 * Constructor for creating {@link KeySet} instances with multiple <i>keys</i>. 37 * 38 * @param firstValue the first <i>key</i> 39 * @param additionalValues additional <i>keys</i> 40 */ 41 public KeySet (final String firstValue, final String... additionalValues) { 42 values = new String[additionalValues.length + 1]; 43 values[0] = firstValue; 44 for (int i = 1; i < values.length; ++i) 45 values[i] = additionalValues[i - 1]; 46 } 47 48 /** 49 * Returns <code>true</code> if one of the <i>keys</i> in the set equals the parameter. 50 * 51 * @param key the <i>key</i> to compare to the set's <i>keys</i> 52 * @return <code>true</code> if there is a match, <code>false</code> if there is not 53 */ 54 public boolean matchKey (final String key) { 55 if (key == null) return false; 56 for (int i = 0; i < values.length; ++i) 57 if (values[i].equals(key)) return true; 58 return false; 59 } 60 61 /** 62 * Returns the primary <i>key</i>, stored at the first position in {@link #values}. 63 * 64 * @return the primary <i>key</i> 65 */ 66 public String getValue () { 67 return values[0]; 68 } 69 70 /** 71 * Returns the <i>key</i> stored at the specified position in {@link #values}. 72 * 73 * @param index the position of the requested <i>key</i> in {@link #values} 74 * @return the <i>key</i> stored at the specified position in {@link #values} 75 */ 76 public String getValue (final int index) { 77 return values[index]; 78 } 79 80 /** 81 * Returns the number of <i>keys</i> in this set. 82 * 83 * @return the number of <i>keys</i> in this set 84 */ 85 public int size () { 86 return values.length; 87 } 88 89 @Override 90 public String toString () { 91 return java.util.Arrays.toString(values); 92 } 93 }