OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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;
15
 
16
import java.text.FieldPosition;
17
import java.text.Format;
18
import java.text.ParsePosition;
19
import java.util.Arrays;
20
import java.util.Iterator;
21
import java.util.List;
22
 
23
/**
24
 * A list of related formats.
25
 *
26
 * @author Sylvain CUAZ
27
 */
28
public class FormatGroup extends Format {
29
 
30
    private final List<? extends Format> formats;
31
    private int formatIndex;
32
 
33
    public FormatGroup(Format... formats) {
34
        this(Arrays.asList(formats));
35
    }
36
 
37
    /**
38
     * Creates a group, which will try to parse with the given formats. format() is done with the
39
     * first format.
40
     *
41
     * @param formats a List of Format.
42
     * @throws IllegalArgumentException if formats is empty.
43
     */
44
    public FormatGroup(final List<? extends Format> formats) {
45
        if (formats.size() == 0)
46
            throw new IllegalArgumentException("formats must not be empty");
47
        this.formats = formats;
48
        this.formatIndex = 0;
49
    }
50
 
51
    public final List<? extends Format> getFormats() {
52
        return this.formats;
53
    }
54
 
55
    @Override
56
    public StringBuffer format(Object newVal, StringBuffer toAppendTo, FieldPosition pos) {
57
        return this.formats.get(this.formatIndex).format(newVal, toAppendTo, pos);
58
    }
59
 
60
    @Override
61
    public Object parseObject(String s, ParsePosition pos) {
62
        if (pos.getErrorIndex() >= 0)
63
            throw new IllegalArgumentException(pos + " has en error at " + pos.getErrorIndex());
64
 
65
        boolean success = false;
66
        Object tmpRes = null;
67
        final ParsePosition tmpPos = new ParsePosition(pos.getIndex());
68
        final Iterator<? extends Format> iter = this.formats.iterator();
69
        while (iter.hasNext() && !success) {
70
            final Format f = iter.next();
71
            mutateTo(tmpPos, pos);
72
            tmpRes = f.parseObject(s, tmpPos);
73
            success = tmpPos.getIndex() != pos.getIndex() && tmpPos.getErrorIndex() < 0;
74
        }
75
 
76
        final Object res;
77
        if (!success) {
78
            // fail with the same as format()
79
            res = this.formats.get(this.formatIndex).parseObject(s, pos);
80
        } else {
81
            res = tmpRes;
82
            mutateTo(pos, tmpPos);
83
        }
84
 
85
        return res;
86
    }
87
 
88
    private void mutateTo(ParsePosition tmpPos, ParsePosition pos) {
89
        tmpPos.setIndex(pos.getIndex());
90
        tmpPos.setErrorIndex(pos.getErrorIndex());
91
    }
92
 
93
    public final int getFormatIndex() {
94
        return this.formatIndex;
95
    }
96
 
97
    public final void setFormatIndex(int formatIndex) {
98
        if (formatIndex < 0 || formatIndex >= this.getFormats().size())
99
            throw new IllegalArgumentException(formatIndex + " out of bounds");
100
        this.formatIndex = formatIndex;
101
    }
102
 
103
}