01: /*
02: * Copyright (C) 2007, 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 14. September 2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.benchmark.xmlfriendly.metric;
12:
13: import com.thoughtworks.xstream.tools.benchmark.Metric;
14: import com.thoughtworks.xstream.tools.benchmark.Product;
15: import com.thoughtworks.xstream.tools.benchmark.Target;
16:
17: import java.io.ByteArrayOutputStream;
18:
19: /**
20: * Determines the amount of a special characters.
21: *
22: * @author Jörg Schaible
23: */
24: public class CharacterCountMetric implements Metric {
25:
26: private final char ch;
27:
28: public CharacterCountMetric(char ch) {
29: this .ch = ch;
30: }
31:
32: public double run(Product product, Target target) throws Exception {
33: return run(product, target.target());
34: }
35:
36: /**
37: *@deprecated since 1.3
38: */
39: public double run(Product product, Object object) throws Exception {
40: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
41: product.serialize(object, buffer);
42: String s = buffer.toString();
43: int counter = 0;
44: for (int i = 0; i < s.length(); i++) {
45: if (s.charAt(i) == ch) {
46: ++counter;
47: }
48: }
49: return counter;
50: }
51:
52: public String toString() {
53: return "Character count for '" + ch + "'";
54: }
55:
56: public String unit() {
57: return "characters";
58: }
59:
60: public boolean biggerIsBetter() {
61: return false;
62: }
63: }
|