EMMA Coverage Report (generated Sun Mar 01 22:06:14 CET 2015)
[all classes][org.h2.util]

COVERAGE SUMMARY FOR SOURCE FILE [SortedProperties.java]

nameclass, %method, %block, %line, %
SortedProperties.java100% (1/1)75%  (6/8)51%  (127/251)45%  (24.2/54)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SortedProperties100% (1/1)75%  (6/8)51%  (127/251)45%  (24.2/54)
store (String): void 0%   (0/1)0%   (0/73)0%   (0/18)
toLines (): String 0%   (0/1)0%   (0/34)0%   (0/5)
getBooleanProperty (Properties, String, boolean): boolean 100% (1/1)75%  (15/20)40%  (2/5)
loadProperties (String): SortedProperties 100% (1/1)76%  (22/29)90%  (7.2/8)
getIntProperty (Properties, String, int): int 100% (1/1)76%  (16/21)40%  (2/5)
SortedProperties (): void 100% (1/1)100% (3/3)100% (1/1)
fromLines (String): SortedProperties 100% (1/1)100% (43/43)100% (6/6)
keys (): Enumeration 100% (1/1)100% (28/28)100% (6/6)

1/*
2 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (http://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6package org.h2.util;
7 
8import java.io.BufferedWriter;
9import java.io.ByteArrayInputStream;
10import java.io.ByteArrayOutputStream;
11import java.io.IOException;
12import java.io.InputStream;
13import java.io.InputStreamReader;
14import java.io.LineNumberReader;
15import java.io.OutputStreamWriter;
16import java.io.PrintWriter;
17import java.io.Writer;
18import java.util.Collections;
19import java.util.Enumeration;
20import java.util.Properties;
21import java.util.TreeMap;
22import java.util.Vector;
23import java.util.Map.Entry;
24import org.h2.store.fs.FileUtils;
25 
26/**
27 * Sorted properties file.
28 * This implementation requires that store() internally calls keys().
29 */
30public class SortedProperties extends Properties {
31 
32    private static final long serialVersionUID = 1L;
33 
34    @Override
35    public synchronized Enumeration<Object> keys() {
36        Vector<String> v = new Vector<String>();
37        for (Object o : keySet()) {
38            v.add(o.toString());
39        }
40        Collections.sort(v);
41        return new Vector<Object>(v).elements();
42    }
43 
44    /**
45     * Get a boolean property value from a properties object.
46     *
47     * @param prop the properties object
48     * @param key the key
49     * @param def the default value
50     * @return the value if set, or the default value if not
51     */
52    public static boolean getBooleanProperty(Properties prop, String key,
53            boolean def) {
54        String value = prop.getProperty(key, "" + def);
55        try {
56            return Boolean.parseBoolean(value);
57        } catch (Exception e) {
58            e.printStackTrace();
59            return def;
60        }
61    }
62 
63    /**
64     * Get an int property value from a properties object.
65     *
66     * @param prop the properties object
67     * @param key the key
68     * @param def the default value
69     * @return the value if set, or the default value if not
70     */
71    public static int getIntProperty(Properties prop, String key, int def) {
72        String value = prop.getProperty(key, "" + def);
73        try {
74            return Integer.decode(value);
75        } catch (Exception e) {
76            e.printStackTrace();
77            return def;
78        }
79    }
80 
81    /**
82     * Load a properties object from a file.
83     *
84     * @param fileName the name of the properties file
85     * @return the properties object
86     */
87    public static synchronized SortedProperties loadProperties(String fileName)
88            throws IOException {
89        SortedProperties prop = new SortedProperties();
90        if (FileUtils.exists(fileName)) {
91            InputStream in = null;
92            try {
93                in = FileUtils.newInputStream(fileName);
94                prop.load(in);
95            } finally {
96                if (in != null) {
97                    in.close();
98                }
99            }
100        }
101        return prop;
102    }
103 
104    /**
105     * Store a properties file. The header and the date is not written.
106     *
107     * @param fileName the target file name
108     */
109    public synchronized void store(String fileName) throws IOException {
110        ByteArrayOutputStream out = new ByteArrayOutputStream();
111        store(out, null);
112        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
113        InputStreamReader reader = new InputStreamReader(in, "ISO8859-1");
114        LineNumberReader r = new LineNumberReader(reader);
115        Writer w;
116        try {
117            w = new OutputStreamWriter(FileUtils.newOutputStream(fileName, false));
118        } catch (Exception e) {
119            throw new IOException(e.toString(), e);
120        }
121        PrintWriter writer = new PrintWriter(new BufferedWriter(w));
122        while (true) {
123            String line = r.readLine();
124            if (line == null) {
125                break;
126            }
127            if (!line.startsWith("#")) {
128                writer.print(line + "\n");
129            }
130        }
131        writer.close();
132    }
133 
134    /**
135     * Convert the map to a list of line in the form key=value.
136     *
137     * @return the lines
138     */
139    public synchronized String toLines() {
140        StringBuilder buff = new StringBuilder();
141        for (Entry<Object, Object> e : new TreeMap<Object, Object>(this).entrySet()) {
142            buff.append(e.getKey()).append('=').append(e.getValue()).append('\n');
143        }
144        return buff.toString();
145    }
146 
147    /**
148     * Convert a String to a map.
149     *
150     * @param s the string
151     * @return the map
152     */
153    public static SortedProperties fromLines(String s) {
154        SortedProperties p = new SortedProperties();
155        for (String line : StringUtils.arraySplit(s, '\n', true)) {
156            int idx = line.indexOf('=');
157            if (idx > 0) {
158                p.put(line.substring(0, idx), line.substring(idx + 1));
159            }
160        }
161        return p;
162    }
163 
164}

[all classes][org.h2.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov