All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fs.commons.desktop.validation.conversion.Converter Maven / Gradle / Ivy

There is a newer version: 0.0.9-3
Show newest version
/*
 * Copyright 2002-2016 Jalal Kiswani.
 *
 * 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 com.fs.commons.desktop.validation.conversion;

import java.util.HashSet;
import java.util.Set;

import javax.swing.ComboBoxModel;
import javax.swing.text.Document;

import com.fs.commons.desktop.validation.Validator;
import com.fs.commons.desktop.validation.builtin.FSValidators;

/**
 * Converts a validator of one type to a validator that works against a
 * different type. In this way, it is possible to write only validators for
 * Strings, but use them against javax.swing.text.Documents, etc.
 *
 * @author Tim Boudreau
 */
public abstract class Converter {
	private static final class Entry {
		private final Class from;
		private final Class to;
		private final Converter converter;

		public Entry(final Class from, final Class to, final Converter converter) {
			this.from = from;
			this.to = to;
			this.converter = converter;
		}

		Converter getConverter() {
			return this.converter;
		}

		public boolean match(final Class from, final Class to) {
			return this.from.equals(from) && this.to.equals(to);
		}
	}

	private static Set registry = new HashSet();

	static {
		Converter. register(String.class, Document.class, new StringToDocumentConverter());
		Converter. register(String.class, ComboBoxModel.class, new StringToComboBoxModelConverter());
	}

	/**
	 * Find a converter to create validators for one type from validators for
	 * another type.
	 * 
	 * @param 
	 *            The type of object we get from a component, such as a
	 *            javax.swing.text.Document
	 * @param 
	 *            The type of object we want to process, such as a
	 *            java.lang.String
	 * @param from
	 *            A class, such as Document.class
	 * @param to
	 *            A class such as String.class
	 * @return An object which can take validators for type From
	 *         and produce validators for type To
	 */
	public static  Converter find(final Class from, final Class to) {
		for (final Entry e : registry) {
			if (e.match(from, to)) {
				return e.getConverter();
			}
		}
		throw new IllegalArgumentException("No registered converter from " + from.getName() + " to " + to.getName());
	}

	/**
	 * Register a converter
	 * 
	 * @param 
	 * @param 
	 * @param from
	 * @param to
	 * @param converter
	 */
	public static  void register(final Class from, final Class to, final Converter converter) {
		registry.add(new Entry(from, to, converter));
	}

	/**
	 * Create a validator for type To by wrapping a validator for type From. For
	 * example, a converter that is a factory for Validators of
	 * javax.swing.text.Documents may be created from a validator that only
	 * handles Strings. Convert would simply return a Validator that first calls
	 * Document.getText(), and passes the result to the Validator.
	 * 
	 * @param from
	 *            The original validator.
	 * @return A validator of the type requested
	 */
	public abstract Validator convert(Validator from);

	public final Validator convert(final Validator... froms) {
		return convert(FSValidators.merge(froms));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy