OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
181 ilm 1
/*
2
 * Copyright 2018 Daniel Gredler
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
 * in compliance with the License. You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software distributed under the License
10
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
 * or implied. See the License for the specific language governing permissions and limitations under
12
 * the License.
13
 */
14
 
15
package uk.org.okapibarcode.util;
16
 
17
import java.nio.charset.Charset;
18
import java.nio.charset.UnsupportedCharsetException;
19
 
20
public class EciMode {
21
 
22
    public static final EciMode NONE = new EciMode(-1, null);
23
 
24
    public final int mode;
25
    public final Charset charset;
26
 
27
    private EciMode(final int mode, final Charset charset) {
28
        this.mode = mode;
29
        this.charset = charset;
30
    }
31
 
32
    public static EciMode of(final String data, final String charsetName, final int mode) {
33
        try {
34
            final Charset charset = Charset.forName(charsetName);
35
            if (charset.canEncode() && charset.newEncoder().canEncode(data)) {
36
                return new EciMode(mode, charset);
37
            } else {
38
                return NONE;
39
            }
40
        } catch (final UnsupportedCharsetException e) {
41
            return NONE;
42
        }
43
    }
44
 
45
    public EciMode or(final String data, final String charsetName, final int mode) {
46
        if (!equals(NONE)) {
47
            return this;
48
        } else {
49
            return of(data, charsetName, mode);
50
        }
51
    }
52
 
53
    @Override
54
    public boolean equals(final Object other) {
55
        return other instanceof EciMode && ((EciMode) other).mode == this.mode;
56
    }
57
 
58
    @Override
59
    public int hashCode() {
60
        return Integer.valueOf(this.mode).hashCode();
61
    }
62
 
63
    @Override
64
    public String toString() {
65
        return "EciMode[mode=" + this.mode + ", charset=" + this.charset + "]";
66
    }
67
}