1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Abstract class for <i>key=value</i> parameters, used in {@link ParameterizedAction}.
|
---|
6 | * <p>
|
---|
7 | * The key ({@link #name}) is a string and the value of class {@code T}. The value can be
|
---|
8 | * converted to and from a string.
|
---|
9 | * @param <T> the value type
|
---|
10 | */
|
---|
11 | public abstract class ActionParameter<T> {
|
---|
12 |
|
---|
13 | private final String name;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Constructs a new ActionParameter.
|
---|
17 | * @param name parameter name (the key)
|
---|
18 | */
|
---|
19 | public ActionParameter(String name) {
|
---|
20 | this.name = name;
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Get the name of this action parameter.
|
---|
25 | * The name is used as a key, to look up values for the parameter.
|
---|
26 | * @return the name of this action parameter
|
---|
27 | */
|
---|
28 | public String getName() {
|
---|
29 | return name;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Get the value type of this action parameter.
|
---|
34 | * @return the value type of this action parameter
|
---|
35 | */
|
---|
36 | public abstract Class<T> getType();
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Convert a given value into a string (serialization).
|
---|
40 | * @param value the value
|
---|
41 | * @return a string representation of the value
|
---|
42 | */
|
---|
43 | public abstract String writeToString(T value);
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Create a value from the given string representation (deserialization).
|
---|
47 | * @param s the string representation of the value
|
---|
48 | * @return the corresponding value object
|
---|
49 | */
|
---|
50 | public abstract T readFromString(String s);
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Simple ActionParameter implementation for string values.
|
---|
54 | */
|
---|
55 | public static class StringActionParameter extends ActionParameter<String> {
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Constructs a new {@code StringActionParameter}.
|
---|
59 | * @param name parameter name (the key)
|
---|
60 | */
|
---|
61 | public StringActionParameter(String name) {
|
---|
62 | super(name);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public Class<String> getType() {
|
---|
67 | return String.class;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public String readFromString(String s) {
|
---|
72 | return s;
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public String writeToString(String value) {
|
---|
77 | return value;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|