001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.domain.product;
028:
029: import org.databene.benerator.GeneratorProxy;
030: import org.databene.benerator.wrapper.UniqueAlternativeGenerator;
031: import org.databene.benerator.wrapper.AlternativeGenerator;
032:
033: /**
034: * Generates EAN8 and EAN13 codes at the configured ratio.<br/>
035: * <br/>
036: * Created: 30.07.2007 21:23:44
037: */
038: public class EANGenerator extends GeneratorProxy<String> {
039:
040: private boolean unique;
041:
042: private boolean dirty;
043:
044: public EANGenerator() {
045: this (false);
046: }
047:
048: public EANGenerator(boolean unique) {
049: super (null);
050: this .unique = unique;
051: this .dirty = true;
052: }
053:
054: // properties ------------------------------------------------------------------------------------------------------
055:
056: public boolean isUnique() {
057: return unique;
058: }
059:
060: public void setUnique(boolean unique) {
061: this .unique = unique;
062: this .dirty = true;
063: }
064:
065: // Generator interface ---------------------------------------------------------------------------------------------
066:
067: public Class<String> getGeneratedType() {
068: return String.class;
069: }
070:
071: public void validate() {
072: if (dirty) {
073: if (unique)
074: super .setSource(new UniqueAlternativeGenerator<String>(
075: String.class, new EAN8Generator(true),
076: new EAN13Generator(true)));
077: else
078: super .setSource(new AlternativeGenerator<String>(
079: String.class, new EAN8Generator(false),
080: new EAN13Generator(false)));
081: super .validate();
082: dirty = false;
083: }
084: }
085:
086: public boolean available() {
087: if (dirty)
088: validate();
089: return super .available();
090: }
091:
092: public String generate() {
093: if (dirty)
094: validate();
095: return super .generate();
096: }
097:
098: // java.lang.Object overrides --------------------------------------------------------------------------------------
099:
100: public String toString() {
101: return getClass().getSimpleName() + (unique ? "[unique]" : "");
102: }
103: }
|