File: Conversion.java

package info (click to toggle)
libjibx1.2-java 1.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,260 kB
  • sloc: java: 75,013; xml: 14,068; makefile: 17
file content (77 lines) | stat: -rw-r--r-- 2,317 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

package example20;

public class Conversion
{
    private Conversion () {}
    
    public static String serializeDollarsCents(int cents) {
        StringBuffer buff = new StringBuffer();
        buff.append(cents / 100);
        int extra = cents % 100;
        if (extra != 0) {
            buff.append('.');
            if (extra < 10) {
                buff.append('0');
            }
            buff.append(extra);
        }
        return buff.toString();
    }
    
    public static int deserializeDollarsCents(String text) {
        if (text == null) {
            return 0;
        } else {
            int split = text.indexOf('.');
            int cents = 0;
            if (split > 0) {
                cents = Integer.parseInt(text.substring(0, split)) * 100;
                text = text.substring(split+1);
            }
            return cents + Integer.parseInt(text);
        }
    }
    
    public static String serializeIntArray(int[] values) {
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < values.length; i++) {
            if (i > 0) {
                buff.append(' ');
            }
            buff.append(values[i]);
        }
        return buff.toString();
    }
    
    private static int[] resizeArray(int[] array, int size) {
        int[] copy = new int[size];
        System.arraycopy(array, 0, copy, 0, Math.min(array.length, size));
        return copy;
    }
    
    public static int[] deserializeIntArray(String text) {
        if (text == null) {
            return new int[0];
        } else {
            int split = 0;
            text = text.trim();
            int fill = 0;
            int[] values = new int[10];
            while (split < text.length()) {
                int base = split;
                split = text.indexOf(' ', split);
                if (split < 0) {
                    split = text.length();
                }
                int value = Integer.parseInt(text.substring(base, split));
                if (fill >= values.length) {
                    values = resizeArray(values, values.length*2);
                }
                values[fill++] = value;
                while (split < text.length() && text.charAt(++split) == ' ');
            }
            return resizeArray(values, fill);
        }
    }
}