File: Parameter.java

package info (click to toggle)
brltty 6.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,776 kB
  • sloc: ansic: 150,447; java: 13,484; sh: 9,667; xml: 5,702; tcl: 2,634; makefile: 2,328; awk: 713; lisp: 366; python: 321; ml: 301
file content (253 lines) | stat: -rw-r--r-- 6,379 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * libbrlapi - A library providing access to braille terminals for applications.
 *
 * Copyright (C) 2006-2025 by
 *   Samuel Thibault <Samuel.Thibault@ens-lyon.org>
 *   Sébastien Hinderer <Sebastien.Hinderer@ens-lyon.org>
 *
 * libbrlapi comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

package org.a11y.brlapi;

public abstract class Parameter extends ParameterComponent {
  private final ConnectionBase clientConnection;

  protected Parameter (ConnectionBase connection) {
    super();
    clientConnection = connection;
  }

  private String parameterLabel = null;
  public String getLabel () {
    synchronized (this) {
      if (parameterLabel == null) {
        parameterLabel = Strings.wordify(
          Strings.replaceAll(getClass().getSimpleName(), "Parameter$", "")
        );
      }
    }

    return parameterLabel;
  }

  private String parameterName = null;
  public String getName () {
    synchronized (this) {
      if (parameterName == null) {
        parameterName = toOperandName(getLabel());
      }
    }

    return parameterName;
  }

  public abstract int getParameter ();
  public abstract boolean isGlobal ();

  public boolean hasSubparam () {
    return false;
  }

  public boolean isHidable () {
    return false;
  }

  protected final Object getValue (long subparam) {
    return clientConnection.getParameter(getParameter(), subparam, isGlobal());
  }

  protected final Object getValue () {
    return getValue(DEFAULT_SUBPARAM);
  }

  protected final void setValue (long subparam, Object value) {
    clientConnection.setParameter(getParameter(), subparam, isGlobal(), value);
  }

  public Object get (long subparam) {
    return null;
  }

  public String toString (long subparam) {
    return toString(get(subparam));
  }

  public Object get () {
    return get(DEFAULT_SUBPARAM);
  }

  @Override
  public String toString () {
    return toString(get());
  }

  public interface Settable {
  }

  public final boolean isSettable () {
    return this instanceof Settable;
  }

  public interface StringSettable extends Settable {
    public void set (long subparam, String value) throws SyntaxException;
  }

  public interface BooleanSettable extends Settable {
    public void set (long subparam, boolean value);
  }

  public interface ByteSettable extends Settable {
    public void set (long subparam, byte value);

    public default byte getMinimum () {
      return 0;
    }

    public default byte getMaximum () {
      return Byte.MAX_VALUE;
    }
  }

  public interface ShortSettable extends Settable {
    public void set (long subparam, short value);

    public default short getMinimum () {
      return 0;
    }

    public default short getMaximum () {
      return Short.MAX_VALUE;
    }
  }

  public interface IntSettable extends Settable {
    public void set (long subparam, int value);

    public default int getMinimum () {
      return 0;
    }

    public default int getMaximum () {
      return Integer.MAX_VALUE;
    }
  }

  public interface LongSettable extends Settable {
    public void set (long subparam, long value);

    public default long getMinimum () {
      return 0;
    }

    public default long getMaximum () {
      return Long.MAX_VALUE;
    }
  }

  protected final String getParseDescription () {
    return getName() + " value";
  }

  public void set (long subparam, String value) throws OperandException {
    if (!isSettable()) {
      throw new SemanticException("parameter is not settable: %s", getName());
    }

    if (this instanceof StringSettable) {
      StringSettable settable = (StringSettable)this;
      settable.set(subparam, value);
      return;
    }

    if (this instanceof BooleanSettable) {
      BooleanSettable settable = (BooleanSettable)this;
      settable.set(subparam, Parse.asBoolean(getParseDescription(), value));
      return;
    }

    if (this instanceof ByteSettable) {
      ByteSettable settable = (ByteSettable)this;
      settable.set(subparam, Parse.asByte(getParseDescription(), value, settable.getMinimum(), settable.getMaximum()));
      return;
    }

    if (this instanceof ShortSettable) {
      ShortSettable settable = (ShortSettable)this;
      settable.set(subparam, Parse.asShort(getParseDescription(), value, settable.getMinimum(), settable.getMaximum()));
      return;
    }

    if (this instanceof IntSettable) {
      IntSettable settable = (IntSettable)this;
      settable.set(subparam, Parse.asInt(getParseDescription(), value, settable.getMinimum(), settable.getMaximum()));
      return;
    }

    if (this instanceof LongSettable) {
      LongSettable settable = (LongSettable)this;
      settable.set(subparam, Parse.asLong(getParseDescription(), value, settable.getMinimum(), settable.getMaximum()));
      return;
    }

    {
      Class<?> type = Settable.class;

      for (Class<?> i : getClass().getInterfaces()) {
        if (type.isAssignableFrom(i)) {
          type = i;
          break;
        }
      }

      throw new UnsupportedOperationException(
        String.format(
          "set not supported: %s: %s", getName(), type.getSimpleName()
        )
      );
    }
  }

  public void set (String value) throws OperandException {
    set(DEFAULT_SUBPARAM, value);
  }

  public final static class WatcherHandle implements AutoCloseable {
    private long watchIdentifier;

    private WatcherHandle (long identifier) {
      watchIdentifier = identifier;
    }

    @Override
    public void close () {
      synchronized (this) {
        if (watchIdentifier != 0) {
          ConnectionBase.unwatchParameter(watchIdentifier);
          watchIdentifier = 0;
        }
      }
    }
  }

  public final WatcherHandle watch (long subparam, ParameterWatcher watcher) {
    return new WatcherHandle(
      clientConnection.watchParameter(
        getParameter(), subparam, isGlobal(), watcher
      )
    );
  }

  public final WatcherHandle watch (ParameterWatcher watcher) {
    return watch(DEFAULT_SUBPARAM, watcher);
  }
}