001/* 002 * Copyright 2006 - 2013 003 * Stefan Balev <stefan.balev@graphstream-project.org> 004 * Julien Baudry <julien.baudry@graphstream-project.org> 005 * Antoine Dutot <antoine.dutot@graphstream-project.org> 006 * Yoann Pigné <yoann.pigne@graphstream-project.org> 007 * Guilhelm Savin <guilhelm.savin@graphstream-project.org> 008 * 009 * GraphStream is a library whose purpose is to handle static or dynamic 010 * graph, create them from scratch, file or any source and display them. 011 * 012 * This program is free software distributed under the terms of two licenses, the 013 * CeCILL-C license that fits European law, and the GNU Lesser General Public 014 * License. You can use, modify and/ or redistribute the software under the terms 015 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following 016 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by 017 * the Free Software Foundation, either version 3 of the License, or (at your 018 * option) any later version. 019 * 020 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 021 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 022 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 023 * 024 * You should have received a copy of the GNU Lesser General Public License 025 * along with this program. If not, see <http://www.gnu.org/licenses/>. 026 * 027 * The fact that you are presently reading this means that you have had 028 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. 029 */ 030package org.graphstream.util.cumulative; 031 032import java.util.LinkedList; 033 034public class CumulativeSpells { 035 public static class Spell { 036 private double start; 037 private double end; 038 039 private boolean startOpen; 040 private boolean endOpen; 041 042 private boolean closed; 043 044 private Object data; 045 046 public Spell(double start, boolean startOpen, double end, 047 boolean endOpen) { 048 this.start = start; 049 this.startOpen = startOpen; 050 this.end = end; 051 this.endOpen = endOpen; 052 053 this.closed = false; 054 } 055 056 public Spell(double start, double end) { 057 this(start, false, end, false); 058 } 059 060 public Spell(double start) { 061 this(start, false, start, false); 062 } 063 064 public double getStartDate() { 065 return start; 066 } 067 068 public double getEndDate() { 069 return end; 070 } 071 072 public boolean isStartOpen() { 073 return startOpen; 074 } 075 076 public boolean isEndOpen() { 077 return endOpen; 078 } 079 080 public boolean isStarted() { 081 return !Double.isNaN(start); 082 } 083 084 public boolean isEnded() { 085 return closed; 086 } 087 088 public void setStartOpen(boolean open) { 089 startOpen = open; 090 } 091 092 public void setEndOpen(boolean open) { 093 endOpen = open; 094 } 095 096 public Object getAttachedData() { 097 return data; 098 } 099 100 public void setAttachedData(Object data) { 101 this.data = data; 102 } 103 104 public String toString() { 105 String str = ""; 106 107 if (isStarted()) { 108 str += isStartOpen() ? "]" : "["; 109 str += start + "; "; 110 } else 111 str += "[...; "; 112 113 if (isEnded()) { 114 str += end; 115 str += isEndOpen() ? "[" : "]"; 116 } else 117 str += "...]"; 118 119 return str; 120 } 121 } 122 123 LinkedList<Spell> spells; 124 double currentDate; 125 126 public CumulativeSpells() { 127 this.spells = new LinkedList<Spell>(); 128 currentDate = Double.NaN; 129 } 130 131 public Spell startSpell(double date) { 132 Spell s = new Spell(date); 133 spells.add(s); 134 135 return s; 136 } 137 138 public void updateCurrentSpell(double date) { 139 if (spells.size() > 0 && !Double.isNaN(currentDate)) { 140 Spell s = spells.getLast(); 141 142 if (!s.closed) 143 s.end = currentDate; 144 } 145 146 currentDate = date; 147 } 148 149 public Spell closeSpell() { 150 if (spells.size() > 0) { 151 Spell s = spells.getLast(); 152 153 if (!s.closed) { 154 s.closed = true; 155 return s; 156 } 157 } 158 159 return null; 160 } 161 162 public Spell getCurrentSpell() { 163 Spell s = spells.getLast(); 164 165 if (s == null) 166 return null; 167 168 return s.closed ? null : s; 169 } 170 171 public Spell getSpell(int i) { 172 return spells.get(i); 173 } 174 175 public int getSpellCount() { 176 return spells.size(); 177 } 178 179 public Spell getOrCreateSpell(double date) { 180 Spell s = getCurrentSpell(); 181 182 if (s == null) 183 s = startSpell(date); 184 185 return s; 186 } 187 188 public boolean isEternal() { 189 return spells.size() == 1 && !spells.get(0).isStarted() 190 && !spells.get(0).isEnded(); 191 } 192 193 public String toString() { 194 StringBuilder buffer = new StringBuilder(); 195 196 buffer.append("{"); 197 198 for (int i = 0; i < spells.size(); i++) { 199 if (i > 0) 200 buffer.append(", "); 201 202 buffer.append(spells.get(i).toString()); 203 } 204 205 buffer.append("}"); 206 207 return buffer.toString(); 208 } 209}