View Javadoc

1   package org.jscsi.target.settings.entry;
2   
3   
4   import org.jscsi.target.TargetServer;
5   import org.jscsi.target.settings.BooleanResultFunction;
6   import org.jscsi.target.settings.KeySet;
7   import org.jscsi.target.settings.NegotiationStatus;
8   import org.jscsi.target.settings.NegotiationType;
9   import org.jscsi.target.settings.TextParameter;
10  
11  
12  /**
13   * An {@link Entry} sub-class for boolean parameters.
14   * <p>
15   * During text parameter negotiation, boolean values are encoded as <code>Yes</code> and <code>No</code>, meaning
16   * <code>true</code> and <code>false</code>, respectively.
17   * <p>
18   * The default or negotiated value can be accessed via the {@link #getBooleanValue()} method.
19   * 
20   * @see Entry
21   * @author Andreas Ergenzinger
22   */
23  public final class BooleanEntry extends Entry {
24  
25      private final boolean negotiationValue;
26      private final BooleanResultFunction resultFunction;
27  
28      /**
29       * The {@link BooleanEntry} constructor.
30       * 
31       * @param keySet contains all relevant keys
32       * @param use determines under which circumstances the parameter may be negotiated
33       * @param negotiationStatus indicates whether there is a default value or if the parameter must be negotiated
34       * @param negotiationValue together with the <i>resultFunction</i> parameter this value indicates the values
35       *            supported by the jSCSI Target
36       * @param resultFunction the result function to use during negotiations
37       * @param defaultValue the default value or <code>null</code>
38       */
39      public BooleanEntry (final KeySet keySet, final Use use, final NegotiationStatus negotiationStatus, final boolean negotiationValue, final BooleanResultFunction resultFunction, final Boolean defaultValue) {
40          super(keySet, NegotiationType.NEGOTIATED, use, negotiationStatus, defaultValue);
41          this.negotiationValue = negotiationValue;
42          this.resultFunction = resultFunction;
43      }
44  
45      @Override
46      protected Object parseOffer (TargetServer target, final String values) {
47          return TextParameter.parseBooleanValue(values);
48      }
49  
50      @Override
51      protected boolean inProtocolValueRange (final Object values) {
52          if (values instanceof Boolean) return true;
53          return false;// should never happen
54      }
55  
56      @Override
57      protected void processDeclaration (final Object values) {
58          // there are no declarations, see constructor
59      }
60  
61      @Override
62      protected String processNegotiation (final Object values) {
63          final boolean request = (Boolean) values;
64          final boolean result = resultFunction.getResult(request, negotiationValue);
65          value = result;
66          return TextParameter.booleanToTextValue(result);
67      }
68  
69      @Override
70      public Boolean getBooleanValue () {
71          return (Boolean) value;
72      }
73  
74      @Override
75      public Entry copy () {
76          final BooleanEntry e = new BooleanEntry(keySet, use, negotiationStatus, negotiationValue, resultFunction, (Boolean) value);
77          e.alreadyNegotiated = this.alreadyNegotiated;
78          return e;
79      }
80  
81  }