org.jrimum.bopepo.pdf.CodigoDeBarras.java Source code

Java tutorial

Introduction

Here is the source code for org.jrimum.bopepo.pdf.CodigoDeBarras.java

Source

/*
 * Copyright 2011 JRimum Project
 * 
 * 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.
 * 
 * Created at: 14/04/2011 - 14:49:07
 * 
 * ================================================================================
 * 
 * Direitos autorais 2011 JRimum Project
 * 
 * Licenciado sob a Licena Apache, Verso 2.0 ("LICENA"); voc no pode usar
 * esse arquivo exceto em conformidade com a esta LICENA. Voc pode obter uma
 * cpia desta LICENA em http://www.apache.org/licenses/LICENSE-2.0 A menos que
 * haja exigncia legal ou acordo por escrito, a distribuio de software sob
 * esta LICENA se dar COMO EST??, SEM GARANTIAS OU CONDIES DE QUALQUER
 * TIPO, sejam expressas ou tcitas. Veja a LICENA para a redao especfica a
 * reger permisses e limitaes sob esta LICENA.
 * 
 * Criado em: 14/04/2011 - 14:49:07
 * 
 */

package org.jrimum.bopepo.pdf;

import static java.lang.String.format;

import java.awt.Color;
import java.awt.Image;

import org.jrimum.utilix.Exceptions;
import org.jrimum.utilix.Objects;
import org.jrimum.utilix.text.Strings;

import com.lowagie.text.pdf.BarcodeInter25;

/**
 * Classe geradora  de cdigo de barras no padro FEBRABAN.
 * 
 * @author <a href="http://gilmatryx.googlepages.com/">Gilmar P.S.L.</a>
 * @author <a href="mailto:romulomail@gmail.com">Rmulo Augusto</a>
 *
 * @version 0.2.3
 * 
 * @since 0.2
 */
public class CodigoDeBarras {

    private String codigo;

    /**
     * Classe no instancivel
     * 
     * @throws IllegalStateException
     *             Caso haja alguma tentativa de utilizao deste construtor.
     *             
     * @since 0.2
     */
    @SuppressWarnings("unused")
    private CodigoDeBarras() {

        Exceptions.throwIllegalStateException("Instanciao no permitida!");
    }

    public CodigoDeBarras(String codigo) {

        checkCodigo(codigo);

        this.codigo = codigo;
    }

    public static CodigoDeBarras valueOf(String codigo) {
        checkCodigo(codigo);
        return new CodigoDeBarras(codigo);
    }

    public String write() {

        return codigo;
    }

    public Image toImage() {

        // Montando o cdigo de barras.
        BarcodeInter25 barCode = new BarcodeInter25();
        barCode.setCode(this.write());

        barCode.setExtended(true);
        barCode.setBarHeight(35);
        barCode.setFont(null);
        barCode.setN(3);

        return barCode.createAwtImage(Color.BLACK, Color.WHITE);
    }

    private static void checkCodigo(String str) {

        Objects.checkNotNull(str, "Cdigo nulo!");
        Strings.checkNotBlank(str, format("Cdigo ausente! str = \"%s\"", str));
        Strings.checkNotNumeric(str, format("Cdigo no contm apenas nmeros! str = \"%s\"", str));
        Objects.checkArgument(str.length() == 44,
                format("Cdigo com tamanho diferente de 44 dgitos! str = \"%s\"", str));
    }
}