OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 28 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
28 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
28 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.text;
15
 
16
import org.openconcerto.utils.i18n.I18nUtils;
17
 
18
import java.text.FieldPosition;
19
import java.text.Format;
20
import java.text.ParsePosition;
182 ilm 21
import java.util.Arrays;
28 ilm 22
import java.util.Locale;
23
import java.util.ResourceBundle;
24
 
182 ilm 25
import net.jcip.annotations.Immutable;
26
 
27
@Immutable
28 ilm 28
public class BooleanFormat extends Format {
29
    static private final boolean[] BOOLEAN_VALUES = new boolean[] { false, true };
30
    static private final BooleanFormat NUMBER_INSTANCE = new BooleanFormat("0", "1");
31
 
32
    static private String[] getFormattedBooleans(final Locale l, final boolean yesNo) {
33
        final ResourceBundle bundle = ResourceBundle.getBundle(I18nUtils.RSRC_BASENAME, l);
34
        final String[] res = new String[BOOLEAN_VALUES.length];
35
        for (int i = 0; i < res.length; i++) {
36
            final boolean b = BOOLEAN_VALUES[i];
37
            res[i] = bundle.getString(yesNo ? I18nUtils.getYesNoKey(b) : I18nUtils.getBooleanKey(b));
38
        }
39
        return res;
40
    }
41
 
42
    static public BooleanFormat createYesNo(final Locale l) {
43
        return new BooleanFormat(getFormattedBooleans(l, true));
44
    }
45
 
182 ilm 46
    static public BooleanFormat createTrueFalse(final Locale l) {
47
        return new BooleanFormat(getFormattedBooleans(l, false));
48
    }
49
 
28 ilm 50
    static public final BooleanFormat getNumberInstance() {
51
        return NUMBER_INSTANCE;
52
    }
53
 
182 ilm 54
    static public final BooleanFormat[] getAll(final Locale l) {
55
        return new BooleanFormat[] { createTrueFalse(l), createYesNo(l), getNumberInstance() };
56
    }
57
 
28 ilm 58
    private final String[] formattedValues;
59
 
60
    public BooleanFormat() {
61
        this(getFormattedBooleans(Locale.getDefault(), false));
62
    }
63
 
64
    public BooleanFormat(final String falseValue, final String trueValue) {
65
        this(new String[] { falseValue, trueValue });
66
    }
67
 
182 ilm 68
    // not public because parameter isn't copied
28 ilm 69
    private BooleanFormat(final String[] formattedValues) {
70
        this.formattedValues = formattedValues;
71
    }
72
 
73
    public final String format(boolean b) {
74
        return this.formattedValues[b ? 1 : 0];
75
    }
76
 
77
    protected final String format(Boolean b) {
78
        return b == null ? "" : format(b.booleanValue());
79
    }
80
 
81
    @Override
82
    public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
83
        if (obj == null) {
84
            return toAppendTo;
85
        } else if (obj instanceof Boolean) {
86
            return toAppendTo.append(this.format((Boolean) obj));
87
        } else {
88
            throw new IllegalArgumentException("Not a boolean : " + obj);
89
        }
90
    }
91
 
92
    @Override
93
    public Object parseObject(String source, ParsePosition pos) {
94
        for (final boolean b : BOOLEAN_VALUES) {
95
            final String value = format(b);
96
            if (source.substring(pos.getIndex()).startsWith(value)) {
97
                pos.setIndex(pos.getIndex() + value.length());
98
                return b;
99
            }
100
        }
101
        pos.setErrorIndex(pos.getIndex());
102
        return null;
103
    }
182 ilm 104
 
105
    @Override
106
    public int hashCode() {
107
        final int prime = 31;
108
        int result = 1;
109
        result = prime * result + Arrays.hashCode(this.formattedValues);
110
        return result;
111
    }
112
 
113
    @Override
114
    public boolean equals(Object obj) {
115
        if (this == obj)
116
            return true;
117
        if (obj == null)
118
            return false;
119
        if (getClass() != obj.getClass())
120
            return false;
121
        final BooleanFormat other = (BooleanFormat) obj;
122
        return Arrays.equals(this.formattedValues, other.formattedValues);
123
    }
124
 
125
    @Override
126
    public String toString() {
127
        return this.getClass().getSimpleName() + " with " + Arrays.asList(this.formattedValues);
128
    }
28 ilm 129
}