Java tutorial
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. Portions Copyrighted 2012 Daniel Huss. * * The contents of this file are subject to the terms of either the GNU General Public License Version 2 only ("GPL") or * the Common Development and Distribution License("CDDL") (collectively, the "License"). You may not use this file * except in compliance with the License. You can obtain a copy of the License at * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific language * governing permissions and limitations under the License. When distributing the software, include this License Header * Notice in each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this particular * file as subject to the "Classpath" exception as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License Header, with the fields enclosed by * brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]" * * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original Software is Sun Microsystems, Inc. Portions * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL or only the GPL Version 2, indicate your * decision by adding "[Contributor] elects to include this software in this distribution under the [CDDL or GPL Version * 2] license." If you do not indicate a single choice of license, a recipient has the option to distribute your version * of this file under either the CDDL, the GPL Version 2 or to extend the choice of license to its licensees as provided * above. However, if you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then the option * applies only if the new code is made subject to such option by the copyright holder. */ package de.unentscheidbar.validation.builtin; import java.io.File; import java.util.EnumMap; import java.util.Map; import java.util.Objects; import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; import org.apache.commons.lang3.text.WordUtils; import de.unentscheidbar.validation.Severity; import de.unentscheidbar.validation.ValidationMessage; import de.unentscheidbar.validation.ValidationResult; import de.unentscheidbar.validation.swing.Validation; /** * * @author Tim Boudreau * @author Daniel Huss */ public final class FilePropertiesValidator extends EmptyStringAcceptingValidator { private static final long serialVersionUID = Validation.VERSION; private final Type type; private static final Map<Type, FilePropertiesValidator> INSTANCES = instances(); private static Map<Type, FilePropertiesValidator> instances() { EnumMap<Type, FilePropertiesValidator> map = new EnumMap<>(Type.class); for (Type type : Type.values()) { map.put(type, new FilePropertiesValidator(type)); } return map; } public static FilePropertiesValidator instance(Type type) { Objects.requireNonNull(type, "type"); return INSTANCES.get(type); } private FilePropertiesValidator(Type type) { Objects.requireNonNull(type, "type"); this.type = type; } public static enum Id implements ValidationMessage.Id { FILE_DOES_NOT_EXIST, FILE_IS_NOT_A_DIRECTORY, FILE_IS_NOT_A_FILE, FILE_EXISTS; } @Override protected void validateNonEmptyString(ValidationResult result, String model) { if (!type.validate(new File(model))) result.add(type.messageId(), Severity.ERROR, model, type); } @ParametersAreNonnullByDefault public static enum Type { MUST_EXIST(Id.FILE_DOES_NOT_EXIST) { @Override boolean validate(File file) { return file.exists(); } }, MUST_NOT_EXIST(Id.FILE_EXISTS) { @Override boolean validate(File file) { return !file.exists(); } }, MUST_BE_DIRECTORY(Id.FILE_IS_NOT_A_DIRECTORY) { @Override boolean validate(File file) { return file.isDirectory(); } }, MUST_BE_FILE(Id.FILE_IS_NOT_A_FILE) { @Override boolean validate(File file) { return file.isFile(); } }; abstract boolean validate(File file); private final ValidationMessage.Id messageId; private Type(ValidationMessage.Id messageId) { this.messageId = messageId; } ValidationMessage.Id messageId() { return messageId; } } @Override public @Nonnull String toString() { return "File " + WordUtils.uncapitalize(type.toString().replace('_', ' ')); } }