← joda-money  /  src/main/java/org/joda/money/format/MultiPrinterParser.java

1
/*
2
 *  Copyright 2009-present, Stephen Colebourne
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 */
16
package org.joda.money.format;
17
18
import java.io.IOException;
19
import java.io.Serializable;
20
import java.util.Arrays;
21
22
import org.joda.money.BigMoney;
23
24
/**
25
 * Prints and parses multiple printers/parsers.
26
 * <p>
27
 * This class is immutable and thread-safe.
28
 */
29
final class MultiPrinterParser implements MoneyPrinter, MoneyParser, Serializable {
30
31
    /** Serialization version. */
32
    private static final long serialVersionUID = 1L;
33
34
    /**
35
     * The printers.
36
     */
37
    private final MoneyPrinter[] printers;
38
    /**
39
     * The parsers.
40
     */
41
    private final MoneyParser[] parsers;
42
43
    /**
44
     * Constructor.
45
     * @param printers  the printers, not null
46
     */
47
    MultiPrinterParser(MoneyPrinter[] printers, MoneyParser[] parsers) {
48
        this.printers = printers;
49
        this.parsers = parsers;
50
    }
51
52
    //-----------------------------------------------------------------------
53
    boolean isPrinter() {
54
        return !Arrays.asList(printers).contains(null);
55
    }
56
57
    boolean isParser() {
58
        return !Arrays.asList(parsers).contains(null);
59
    }
60
61
    void appendTo(MoneyFormatterBuilder builder) {
62
        for (var i = 0; i < printers.length; i++) {
63
            builder.append(printers[i], parsers[i]);
64
        }
65
    }
66
67
    //-----------------------------------------------------------------------
68
    @Override
69
    public void print(MoneyPrintContext context, Appendable appendable, BigMoney money) throws IOException {
70
        for (MoneyPrinter printer : printers) {
71
            printer.print(context, appendable, money);
72
        }
73
    }
74
75
    @Override
76
    public void parse(MoneyParseContext context) {
77
        for (MoneyParser parser : parsers) {
78
            parser.parse(context);
79
            if (context.isError()) {
80
                break;
81
            }
82
        }
83
    }
84
85
    @Override
86
    public String toString() {
87
        var buf1 = new StringBuilder();
88
        if (isPrinter()) {
89
            for (MoneyPrinter printer : printers) {
90
                buf1.append(printer.toString());
91
            }
92
        }
93
        var buf2 = new StringBuilder();
94
        if (isParser()) {
95
            for (MoneyParser parser : parsers) {
96
                buf2.append(parser.toString());
97
            }
98
        }
99
        var str1 = buf1.toString();
100
        var str2 = buf2.toString();
101
        if (isPrinter() && !isParser()) {
102
            return str1;
103
        } else if (isParser() && !isPrinter()) {
104
            return str2;
105
        } else if (str1.equals(str2)) {
106
            return str1;
107
        } else {
108
            return str1 + ":" + str2;
109
        }
110
    }
111
112
}
113