001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.tutorial.performance;
032:
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: import com.jgoodies.validation.ValidationResult;
037: import com.jgoodies.validation.util.PropertyValidationSupport;
038: import com.jgoodies.validation.util.ValidationUtils;
039:
040: /**
041: * Validates containers, performs simple validation on the client-side,
042: * and caches valid container numbers.
043: *
044: * @author Karsten Lentzsch
045: * @version $Revision: 1.6 $
046: */
047: class ContainerValidatorWithCache extends ContainerValidator {
048:
049: /**
050: * Maps container numbers to Booleans.
051: *
052: * @see #isValidContainerNumber(String)
053: */
054: private final Map<String, Boolean> cachedValidationResults;
055:
056: // Instance Creation ******************************************************
057:
058: /**
059: * Constructs a ContainerValidator.
060: */
061: ContainerValidatorWithCache(String prefix) {
062: super (prefix);
063: cachedValidationResults = new HashMap<String, Boolean>();
064: }
065:
066: // Validation *************************************************************
067:
068: /**
069: * Validates this Validator's Container and returns the result
070: * as an instance of {@link ValidationResult}.
071: *
072: * @param container the container to be validated
073: * @return the ValidationResult of the container validation
074: */
075: @Override
076: public ValidationResult validate(Container container) {
077: PropertyValidationSupport support = new PropertyValidationSupport(
078: container, prefix);
079:
080: String containerNo = container.getContainerNo();
081: System.out.println("Client validation: " + containerNo);
082: if (ValidationUtils.isBlank(containerNo))
083: support.addError("Container No", "is mandatory");
084: else if (!ValidationUtils.hasBoundedLength(containerNo, 5, 5))
085: support.addError("Container No", "length must be 5");
086: else if (!isValidContainerNumber(containerNo))
087: support.addError("Container No", "invalid");
088:
089: System.out.println();
090: return support.getResult();
091: }
092:
093: private boolean isValidContainerNumber(String containerNumber) {
094: Boolean result = cachedValidationResults.get(containerNumber);
095: if (result == null) {
096: boolean valid = BackendService
097: .isValidContainerNumber(containerNumber);
098: result = Boolean.valueOf(valid);
099: cachedValidationResults.put(containerNumber, result);
100: }
101: return result.booleanValue();
102: }
103:
104: }
|