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.Collections; 033import java.util.HashMap; 034 035import org.graphstream.util.cumulative.CumulativeSpells.Spell; 036 037public class CumulativeAttributes { 038 boolean nullAttributesAreErrors; 039 HashMap<String, CumulativeSpells> data; 040 double date; 041 042 public CumulativeAttributes(double date) { 043 data = new HashMap<String, CumulativeSpells>(); 044 } 045 046 public Object get(String key) { 047 CumulativeSpells o = data.get(key); 048 049 if (o != null) { 050 Spell s = o.getCurrentSpell(); 051 return s == null ? null : s.getAttachedData(); 052 } 053 054 return null; 055 } 056 057 public Object getAny(String key) { 058 CumulativeSpells o = data.get(key); 059 060 if (o != null) { 061 Spell s = o.getSpell(0); 062 return s == null ? null : s.getAttachedData(); 063 } 064 065 return null; 066 } 067 068 public Iterable<String> getAttributes() { 069 return data.keySet(); 070 } 071 072 @SuppressWarnings("unchecked") 073 public Iterable<Spell> getAttributeSpells(String key) { 074 CumulativeSpells o = data.get(key); 075 076 if (o != null) 077 return Collections.unmodifiableList(o.spells); 078 079 return Collections.EMPTY_LIST; 080 } 081 082 public int getAttributesCount() { 083 return data.size(); 084 } 085 086 public void set(String key, Object value) { 087 CumulativeSpells spells = data.get(key); 088 089 if (spells == null) { 090 spells = new CumulativeSpells(); 091 data.put(key, spells); 092 } 093 094 Spell s = spells.closeSpell(); 095 096 if (s != null) 097 s.setEndOpen(true); 098 099 s = spells.startSpell(date); 100 s.setAttachedData(value); 101 } 102 103 public void remove(String key) { 104 CumulativeSpells spells = data.get(key); 105 106 if (spells == null) 107 return; 108 109 spells.closeSpell(); 110 } 111 112 public void remove() { 113 for (CumulativeSpells spells : data.values()) 114 spells.closeSpell(); 115 } 116 117 public void updateDate(double date) { 118 this.date = date; 119 120 for (CumulativeSpells spells : data.values()) 121 spells.updateCurrentSpell(date); 122 } 123 124 public String toString() { 125 StringBuilder buffer = new StringBuilder(); 126 127 buffer.append("("); 128 129 for (String key : data.keySet()) { 130 buffer.append(key).append(":").append(data.get(key)); 131 } 132 133 buffer.append(")"); 134 135 return buffer.toString(); 136 } 137}