[3175] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[12546] | 4 | /**
|
---|
| 5 | * Abstract class for <i>key=value</i> parameters, used in {@link ParameterizedAction}.
|
---|
| 6 | * <p>
|
---|
[12553] | 7 | * The key ({@link #name}) is a string and the value of class {@code T}. The value can be
|
---|
[12546] | 8 | * converted to and from a string.
|
---|
| 9 | * @param <T> the value type
|
---|
| 10 | */
|
---|
[3175] | 11 | public abstract class ActionParameter<T> {
|
---|
| 12 |
|
---|
| 13 | private final String name;
|
---|
| 14 |
|
---|
[12546] | 15 | /**
|
---|
| 16 | * Constructs a new ActionParameter.
|
---|
| 17 | * @param name parameter name (the key)
|
---|
| 18 | */
|
---|
[3175] | 19 | public ActionParameter(String name) {
|
---|
| 20 | this.name = name;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[12546] | 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 | */
|
---|
[3175] | 28 | public String getName() {
|
---|
| 29 | return name;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[12546] | 32 | /**
|
---|
| 33 | * Get the value type of this action parameter.
|
---|
| 34 | * @return the value type of this action parameter
|
---|
| 35 | */
|
---|
[3175] | 36 | public abstract Class<T> getType();
|
---|
| 37 |
|
---|
[12546] | 38 | /**
|
---|
| 39 | * Convert a given value into a string (serialization).
|
---|
| 40 | * @param value the value
|
---|
| 41 | * @return a string representation of the value
|
---|
| 42 | */
|
---|
[3175] | 43 | public abstract String writeToString(T value);
|
---|
| 44 |
|
---|
[12546] | 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 | */
|
---|
[3175] | 50 | public abstract T readFromString(String s);
|
---|
| 51 |
|
---|
[12546] | 52 | /**
|
---|
| 53 | * Simple ActionParameter implementation for string values.
|
---|
| 54 | */
|
---|
[3175] | 55 | public static class StringActionParameter extends ActionParameter<String> {
|
---|
| 56 |
|
---|
[12553] | 57 | /**
|
---|
| 58 | * Constructs a new {@code StringActionParameter}.
|
---|
| 59 | * @param name parameter name (the key)
|
---|
| 60 | */
|
---|
[3175] | 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 | }
|
---|