OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Copyright 2018 Daniel Gredler
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package uk.org.okapibarcode.util;

import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;

public class EciMode {

    public static final EciMode NONE = new EciMode(-1, null);

    public final int mode;
    public final Charset charset;

    private EciMode(final int mode, final Charset charset) {
        this.mode = mode;
        this.charset = charset;
    }

    public static EciMode of(final String data, final String charsetName, final int mode) {
        try {
            final Charset charset = Charset.forName(charsetName);
            if (charset.canEncode() && charset.newEncoder().canEncode(data)) {
                return new EciMode(mode, charset);
            } else {
                return NONE;
            }
        } catch (final UnsupportedCharsetException e) {
            return NONE;
        }
    }

    public EciMode or(final String data, final String charsetName, final int mode) {
        if (!equals(NONE)) {
            return this;
        } else {
            return of(data, charsetName, mode);
        }
    }

    @Override
    public boolean equals(final Object other) {
        return other instanceof EciMode && ((EciMode) other).mode == this.mode;
    }

    @Override
    public int hashCode() {
        return Integer.valueOf(this.mode).hashCode();
    }

    @Override
    public String toString() {
        return "EciMode[mode=" + this.mode + ", charset=" + this.charset + "]";
    }
}