FileSinkDGS.java

1
/*
2
 * Copyright 2006 - 2013
3
 *     Stefan Balev     <stefan.balev@graphstream-project.org>
4
 *     Julien Baudry    <julien.baudry@graphstream-project.org>
5
 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
6
 *     Yoann Pign��      <yoann.pigne@graphstream-project.org>
7
 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
8
 * 
9
 * This file is part of GraphStream <http://graphstream-project.org>.
10
 * 
11
 * GraphStream is a library whose purpose is to handle static or dynamic
12
 * graph, create them from scratch, file or any source and display them.
13
 * 
14
 * This program is free software distributed under the terms of two licenses, the
15
 * CeCILL-C license that fits European law, and the GNU Lesser General Public
16
 * License. You can  use, modify and/ or redistribute the software under the terms
17
 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
18
 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
19
 * the Free Software Foundation, either version 3 of the License, or (at your
20
 * option) any later version.
21
 * 
22
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
23
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
24
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
25
 * 
26
 * You should have received a copy of the GNU Lesser General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 * 
29
 * The fact that you are presently reading this means that you have had
30
 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
31
 */
32
package org.graphstream.stream.file;
33
34
import java.awt.Color;
35
import java.io.IOException;
36
import java.io.PrintWriter;
37
import java.lang.reflect.Array;
38
import java.util.HashMap;
39
import java.util.Locale;
40
41
import org.graphstream.graph.CompoundAttribute;
42
43
/**
44
 * File output for the DGS (Dynamic Graph Stream) file format.
45
 */
46
public class FileSinkDGS extends FileSinkBase {
47
	// Attribute
48
49
	/**
50
	 * A shortcut to the output.
51
	 */
52
	protected PrintWriter out;
53
54
	protected String graphName = "";
55
56
	// Command
57
58
	@Override
59
	protected void outputHeader() throws IOException {
60
		out = (PrintWriter) output;
61
62
		out.printf("DGS004%n");
63
64 2 1. outputHeader : changed conditional boundary → NO_COVERAGE
2. outputHeader : negated conditional → NO_COVERAGE
		if (graphName.length() <= 0)
65
			out.printf("null 0 0%n");
66
		else
67
			out.printf("\"%s\" 0 0%n", formatStringForQuoting(graphName));
68
	}
69
70
	@Override
71
	protected void outputEndOfFile() throws IOException {
72
		// NOP
73
	}
74
75
	public void edgeAttributeAdded(String graphId, long timeId, String edgeId,
76
			String attribute, Object value) {
77 1 1. edgeAttributeAdded : removed call to org/graphstream/stream/file/FileSinkDGS::edgeAttributeChanged → NO_COVERAGE
		edgeAttributeChanged(graphId, timeId, edgeId, attribute, null, value);
78
	}
79
80
	public void edgeAttributeChanged(String graphId, long timeId,
81
			String edgeId, String attribute, Object oldValue, Object newValue) {
82
		out.printf("ce \"%s\" %s%n", formatStringForQuoting(edgeId),
83
				attributeString(attribute, newValue, false));
84
	}
85
86
	public void edgeAttributeRemoved(String graphId, long timeId,
87
			String edgeId, String attribute) {
88
		out.printf("ce \"%s\" %s%n", formatStringForQuoting(edgeId),
89
				attributeString(attribute, null, true));
90
	}
91
92
	public void graphAttributeAdded(String graphId, long timeId,
93
			String attribute, Object value) {
94 1 1. graphAttributeAdded : removed call to org/graphstream/stream/file/FileSinkDGS::graphAttributeChanged → NO_COVERAGE
		graphAttributeChanged(graphId, timeId, attribute, null, value);
95
	}
96
97
	public void graphAttributeChanged(String graphId, long timeId,
98
			String attribute, Object oldValue, Object newValue) {
99
		out.printf("cg %s%n", attributeString(attribute, newValue, false));
100
	}
101
102
	public void graphAttributeRemoved(String graphId, long timeId,
103
			String attribute) {
104
		out.printf("cg %s%n", attributeString(attribute, null, true));
105
	}
106
107
	public void nodeAttributeAdded(String graphId, long timeId, String nodeId,
108
			String attribute, Object value) {
109 1 1. nodeAttributeAdded : removed call to org/graphstream/stream/file/FileSinkDGS::nodeAttributeChanged → NO_COVERAGE
		nodeAttributeChanged(graphId, timeId, nodeId, attribute, null, value);
110
	}
111
112
	public void nodeAttributeChanged(String graphId, long timeId,
113
			String nodeId, String attribute, Object oldValue, Object newValue) {
114
		out.printf("cn \"%s\" %s%n", formatStringForQuoting(nodeId),
115
				attributeString(attribute, newValue, false));
116
	}
117
118
	public void nodeAttributeRemoved(String graphId, long timeId,
119
			String nodeId, String attribute) {
120
		out.printf("cn \"%s\" %s%n", formatStringForQuoting(nodeId),
121
				attributeString(attribute, null, true));
122
	}
123
124
	public void edgeAdded(String graphId, long timeId, String edgeId,
125
			String fromNodeId, String toNodeId, boolean directed) {
126
		edgeId = formatStringForQuoting(edgeId);
127
		fromNodeId = formatStringForQuoting(fromNodeId);
128
		toNodeId = formatStringForQuoting(toNodeId);
129
130
		out.printf("ae \"%s\" \"%s\" %s \"%s\"%n", edgeId, fromNodeId,
131 1 1. edgeAdded : negated conditional → NO_COVERAGE
				directed ? ">" : "", toNodeId);
132
	}
133
134
	public void edgeRemoved(String graphId, long timeId, String edgeId) {
135
		out.printf("de \"%s\"%n", formatStringForQuoting(edgeId));
136
	}
137
138
	public void graphCleared(String graphId, long timeId) {
139
		out.printf("cl%n");
140
	}
141
142
	public void nodeAdded(String graphId, long timeId, String nodeId) {
143
		out.printf("an \"%s\"%n", formatStringForQuoting(nodeId));
144
	}
145
146
	public void nodeRemoved(String graphId, long timeId, String nodeId) {
147
		out.printf("dn \"%s\"%n", formatStringForQuoting(nodeId));
148
	}
149
150
	public void stepBegins(String graphId, long timeId, double step) {
151
		out.printf(Locale.US, "st %f%n", step);
152
	}
153
154
	// Utility
155
156
	protected String formatStringForQuoting(String str) {
157 1 1. formatStringForQuoting : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::formatStringForQuoting to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return str.replaceAll("(^|[^\\\\])\"", "$1\\\\\"");
158
	}
159
160
	protected String attributeString(String key, Object value, boolean remove) {
161 2 1. attributeString : negated conditional → NO_COVERAGE
2. attributeString : negated conditional → NO_COVERAGE
		if (key == null || key.length() == 0)
162 1 1. attributeString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::attributeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return null;
163
164 1 1. attributeString : negated conditional → NO_COVERAGE
		if (remove) {
165 1 1. attributeString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::attributeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return String.format(" -\"%s\"", key);
166
		} else {
167 2 1. attributeString : negated conditional → NO_COVERAGE
2. attributeString : negated conditional → NO_COVERAGE
			if (value != null && value.getClass().isArray())
168 1 1. attributeString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::attributeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
				return String.format(" \"%s\":%s", key, arrayString(value));
169
			else
170 1 1. attributeString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::attributeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
				return String.format(" \"%s\":%s", key, valueString(value));
171
		}
172
	}
173
174
	protected String arrayString(Object value) {
175 2 1. arrayString : negated conditional → NO_COVERAGE
2. arrayString : negated conditional → NO_COVERAGE
		if (value != null && value.getClass().isArray()) {
176
			StringBuilder sb = new StringBuilder();
177
			sb.append("{");
178
179 1 1. arrayString : negated conditional → NO_COVERAGE
			if (Array.getLength(value) == 0)
180
				sb.append("\"\"");
181
			else
182
				sb.append(arrayString(Array.get(value, 0)));
183
184 3 1. arrayString : changed conditional boundary → NO_COVERAGE
2. arrayString : Changed increment from 1 to -1 → NO_COVERAGE
3. arrayString : negated conditional → NO_COVERAGE
			for (int i = 1; i < Array.getLength(value); ++i)
185
				sb.append(String
186
						.format(",%s", arrayString(Array.get(value, i))));
187
188
			sb.append("}");
189 1 1. arrayString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::arrayString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return sb.toString();
190
		} else {
191 1 1. arrayString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::arrayString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return valueString(value);
192
		}
193
	}
194
195
	protected String valueString(Object value) {
196 1 1. valueString : negated conditional → NO_COVERAGE
		if (value == null)
197 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return "\"\"";
198
199 1 1. valueString : negated conditional → NO_COVERAGE
		if (value instanceof CharSequence) {
200 1 1. valueString : negated conditional → NO_COVERAGE
			if (value instanceof String)
201 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
				return String.format("\"%s\"",
202
						formatStringForQuoting((String) value));
203
			else
204 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
				return String.format("\"%s\"", (CharSequence) value);
205 1 1. valueString : negated conditional → NO_COVERAGE
		} else if (value instanceof Number) {
206
			Number nval = (Number) value;
207
208 2 1. valueString : negated conditional → NO_COVERAGE
2. valueString : negated conditional → NO_COVERAGE
			if (value instanceof Integer || value instanceof Short
209 2 1. valueString : negated conditional → NO_COVERAGE
2. valueString : negated conditional → NO_COVERAGE
					|| value instanceof Byte || value instanceof Long)
210 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
				return String.format(Locale.US, "%d", nval.longValue());
211
			else
212 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
				return String.format(Locale.US, "%f", nval.doubleValue());
213 1 1. valueString : negated conditional → NO_COVERAGE
		} else if (value instanceof Boolean) {
214 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return String.format(Locale.US, "%b", ((Boolean) value));
215 1 1. valueString : negated conditional → NO_COVERAGE
		} else if (value instanceof Character) {
216 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return String.format("\"%c\"", ((Character) value).charValue());
217 1 1. valueString : negated conditional → NO_COVERAGE
		} else if (value instanceof Object[]) {
218
			Object array[] = (Object[]) value;
219
			int n = array.length;
220
			StringBuffer sb = new StringBuffer();
221
222 2 1. valueString : changed conditional boundary → NO_COVERAGE
2. valueString : negated conditional → NO_COVERAGE
			if (array.length > 0)
223
				sb.append(valueString(array[0]));
224
225 3 1. valueString : changed conditional boundary → NO_COVERAGE
2. valueString : Changed increment from 1 to -1 → NO_COVERAGE
3. valueString : negated conditional → NO_COVERAGE
			for (int i = 1; i < n; i++) {
226
				sb.append(",");
227
				sb.append(valueString(array[i]));
228
			}
229
230 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return sb.toString();
231 1 1. valueString : negated conditional → NO_COVERAGE
		} else if (value instanceof HashMap<?, ?>
232 1 1. valueString : negated conditional → NO_COVERAGE
				|| value instanceof CompoundAttribute) {
233
			HashMap<?, ?> hash;
234
235 1 1. valueString : negated conditional → NO_COVERAGE
			if (value instanceof CompoundAttribute)
236
				hash = ((CompoundAttribute) value).toHashMap();
237
			else
238
				hash = (HashMap<?, ?>) value;
239
240 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return hashToString(hash);
241 1 1. valueString : negated conditional → NO_COVERAGE
		} else if (value instanceof Color) {
242
			Color c = (Color) value;
243 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return String.format("#%02X%02X%02X%02X", c.getRed(), c.getGreen(),
244
					c.getBlue(), c.getAlpha());
245
		} else {
246 1 1. valueString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return String.format("\"%s\"", value.toString());
247
		}
248
	}
249
250
	protected String hashToString(HashMap<?, ?> hash) {
251
		StringBuilder sb = new StringBuilder();
252
253
		sb.append("[ ");
254
255 1 1. hashToString : negated conditional → NO_COVERAGE
		for (Object key : hash.keySet()) {
256
			sb.append(attributeString(key.toString(), hash.get(key), false));
257
			sb.append(",");
258
		}
259
260
		sb.append(']');
261
262 1 1. hashToString : mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::hashToString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return sb.toString();
263
	}
264
}

Mutations

64

1.1
Location : outputHeader
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : outputHeader
Killed by : none
negated conditional → NO_COVERAGE

77

1.1
Location : edgeAttributeAdded
Killed by : none
removed call to org/graphstream/stream/file/FileSinkDGS::edgeAttributeChanged → NO_COVERAGE

94

1.1
Location : graphAttributeAdded
Killed by : none
removed call to org/graphstream/stream/file/FileSinkDGS::graphAttributeChanged → NO_COVERAGE

109

1.1
Location : nodeAttributeAdded
Killed by : none
removed call to org/graphstream/stream/file/FileSinkDGS::nodeAttributeChanged → NO_COVERAGE

131

1.1
Location : edgeAdded
Killed by : none
negated conditional → NO_COVERAGE

157

1.1
Location : formatStringForQuoting
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::formatStringForQuoting to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

161

1.1
Location : attributeString
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : attributeString
Killed by : none
negated conditional → NO_COVERAGE

162

1.1
Location : attributeString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::attributeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

164

1.1
Location : attributeString
Killed by : none
negated conditional → NO_COVERAGE

165

1.1
Location : attributeString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::attributeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

167

1.1
Location : attributeString
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : attributeString
Killed by : none
negated conditional → NO_COVERAGE

168

1.1
Location : attributeString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::attributeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

170

1.1
Location : attributeString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::attributeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

175

1.1
Location : arrayString
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : arrayString
Killed by : none
negated conditional → NO_COVERAGE

179

1.1
Location : arrayString
Killed by : none
negated conditional → NO_COVERAGE

184

1.1
Location : arrayString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : arrayString
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : arrayString
Killed by : none
negated conditional → NO_COVERAGE

189

1.1
Location : arrayString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::arrayString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

191

1.1
Location : arrayString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::arrayString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

196

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

197

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

199

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

200

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

201

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

204

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

205

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

208

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

209

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

210

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

212

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

213

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

214

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

215

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

216

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

217

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

222

1.1
Location : valueString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

225

1.1
Location : valueString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : valueString
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

230

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

231

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

232

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

235

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

240

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

241

1.1
Location : valueString
Killed by : none
negated conditional → NO_COVERAGE

243

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

246

1.1
Location : valueString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::valueString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

255

1.1
Location : hashToString
Killed by : none
negated conditional → NO_COVERAGE

262

1.1
Location : hashToString
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSinkDGS::hashToString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.33