Gridarta Editor
DoubleParameter.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.plugin.parameter;
21 
25 import org.jetbrains.annotations.NotNull;
26 
30 public class DoubleParameter<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractValuePluginParameter<G, A, R, Double> {
31 
35  @NotNull
36  public static final String PARAMETER_TYPE = Double.class.getName();
37 
41  private double min;
42 
48  private double max = 1.0;
49 
53  public DoubleParameter() {
54  super(0.0);
55  }
56 
57  @NotNull
58  @Override
59  public <T> T visit(@NotNull final PluginParameterVisitor<G, A, R, T> visitor) {
60  return visitor.visit(this);
61  }
62 
63  @Override
64  public boolean setStringValue(@NotNull final String stringValue) {
65  final double doubleValue;
66  try {
67  doubleValue = Double.parseDouble(stringValue);
68  } catch (final NumberFormatException ignored) {
69  return false;
70  }
71  if (doubleValue < min) {
72  return false;
73  }
74  if (doubleValue > max) {
75  return false;
76  }
77  setValue(doubleValue);
78  //noinspection FloatingPointEquality
79  assert getValue() == doubleValue;
80  return true;
81  }
82 
83  @Override
84  public void setValue(@NotNull final Double value) {
85  super.setValue(Math.max(Math.min(value, max), min));
86  }
87 
88  @NotNull
89  @Override
90  public String getParameterType() {
91  return PARAMETER_TYPE;
92  }
93 
94  @NotNull
95  @Override
96  public String getStringValue() {
97  return getValue().toString();
98  }
99 
104  public double getMax() {
105  return max;
106  }
107 
112  public void setMax(final double max) {
113  final double newMax = Math.max(min, max);
114  if (this.max == newMax) {
115  return;
116  }
117  this.max = newMax;
118  changed();
119  if (getValue() > this.max) {
120  setValue(this.max);
121  }
122  }
123 
128  public double getMin() {
129  return min;
130  }
131 
136  public void setMin(final double min) {
137  final double newMin = Math.min(min, max);
138  if (this.min == newMin) {
139  return;
140  }
141  this.min = newMin;
142  changed();
143  if (getValue() < this.min) {
144  setValue(this.min);
145  }
146  }
147 
148 }
void setValue(@NotNull final Double value)
void setMin(final double min)
Sets the minimal allowed value.
double getMin()
Returns the minimal allowed value.
Abstract base class for PluginParameter implementations for which the string representation of the va...
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
void setMax(final double max)
Sets the maximal allowed value.
Interface for visitors of PluginParameter instances.
A PluginParameter that holds a double value.
boolean setStringValue(@NotNull final String stringValue)
double getMax()
Returns the maximal allowed value.
static final String PARAMETER_TYPE
The string representation of this parameter type.