2015-04-07 18:26:58 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2019-01-16 18:34:41 +00:00
|
|
|
* (C) Copyright 2009-2020, Arnaud Roques
|
2015-04-07 18:26:58 +00:00
|
|
|
*
|
2016-03-06 16:47:34 +00:00
|
|
|
* Project Info: http://plantuml.com
|
2015-04-07 18:26:58 +00:00
|
|
|
*
|
2017-03-15 19:13:31 +00:00
|
|
|
* If you like this project or if you find it useful, you can support us at:
|
|
|
|
*
|
|
|
|
* http://plantuml.com/patreon (only 1$ per month!)
|
|
|
|
* http://plantuml.com/paypal
|
|
|
|
*
|
2015-04-07 18:26:58 +00:00
|
|
|
* This file is part of PlantUML.
|
|
|
|
*
|
|
|
|
* PlantUML is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PlantUML distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Original Author: Arnaud Roques
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.sequencediagram.teoz;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2015-09-06 17:28:59 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.SymbolContext;
|
2015-05-03 15:36:36 +00:00
|
|
|
|
2015-04-07 18:26:58 +00:00
|
|
|
public class Stairs2 {
|
|
|
|
|
|
|
|
private final List<StairsPosition> ys = new ArrayList<StairsPosition>();
|
2015-05-03 15:36:36 +00:00
|
|
|
private final List<IntegerColored> values = new ArrayList<IntegerColored>();
|
|
|
|
private final Map<Double, IntegerColored> cache = new HashMap<Double, IntegerColored>();
|
2015-04-07 18:26:58 +00:00
|
|
|
|
2015-06-07 10:23:10 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return ys.toString() + " " + values;
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:28:59 +00:00
|
|
|
public void addStep(StairsPosition position, int value, SymbolContext color) {
|
2015-06-07 10:23:10 +00:00
|
|
|
if (value < 0) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
2021-03-04 18:11:16 +00:00
|
|
|
// System.err.println("Stairs2::addStep " + position + " " + value + " color=" +
|
|
|
|
// color);
|
2015-04-07 18:26:58 +00:00
|
|
|
assert ys.size() == values.size();
|
|
|
|
if (ys.size() > 0) {
|
|
|
|
final double lastY = ys.get(ys.size() - 1).getValue();
|
2019-04-21 20:40:01 +00:00
|
|
|
if (position.getValue() <= lastY) {
|
2015-07-11 09:32:49 +00:00
|
|
|
// throw new IllegalArgumentException();
|
|
|
|
return;
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
if (lastY == position.getValue()) {
|
2015-05-03 15:36:36 +00:00
|
|
|
values.set(ys.size() - 1, new IntegerColored(value, color));
|
2015-04-07 18:26:58 +00:00
|
|
|
cache.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ys.add(position);
|
2015-05-03 15:36:36 +00:00
|
|
|
values.add(new IntegerColored(value, color));
|
2015-04-07 18:26:58 +00:00
|
|
|
cache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxValue() {
|
|
|
|
int max = Integer.MIN_VALUE;
|
2015-05-03 15:36:36 +00:00
|
|
|
for (IntegerColored vc : values) {
|
|
|
|
final int v = vc.getValue();
|
2015-04-07 18:26:58 +00:00
|
|
|
if (v > max) {
|
|
|
|
max = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<StairsPosition> getYs() {
|
|
|
|
return Collections.unmodifiableList(ys);
|
|
|
|
}
|
|
|
|
|
2015-05-03 15:36:36 +00:00
|
|
|
public IntegerColored getValue(double y) {
|
|
|
|
IntegerColored resultc = cache.get(y);
|
|
|
|
if (resultc == null) {
|
|
|
|
resultc = getValueSlow(new StairsPosition(y, false));
|
|
|
|
cache.put(y, resultc);
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
2015-05-03 15:36:36 +00:00
|
|
|
return resultc;
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 15:36:36 +00:00
|
|
|
private IntegerColored getValueSlow(StairsPosition y) {
|
2015-04-07 18:26:58 +00:00
|
|
|
final int idx = Collections.binarySearch(ys, y);
|
|
|
|
if (idx >= 0) {
|
|
|
|
return values.get(idx);
|
|
|
|
}
|
|
|
|
final int insertPoint = -idx - 1;
|
|
|
|
if (insertPoint == 0) {
|
2015-05-03 15:36:36 +00:00
|
|
|
return new IntegerColored(0, null);
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
return values.get(insertPoint - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLastValue() {
|
|
|
|
final int size = values.size();
|
|
|
|
if (size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-03 15:36:36 +00:00
|
|
|
return values.get(size - 1).getValue();
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|