/******************************************************************************* * Copyright (c) 2007, 2011 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. * * Contributors: * Oracle - initial API and implementation ******************************************************************************/ package org.eclipse.jpt.common.utility.internal; import java.io.Serializable; /** * Used by various "pluggable" classes to transform objects * into strings and vice versa. * * If anyone can come up with a better class name * and/or method name, I would love to hear it. ~bjv */ public interface BidiStringConverter extends StringConverter { /** * Convert the specified string into an object. * The semantics of "convert to object" is determined by the * contract between the client and the server. * Typically, if the string is null, null is returned. */ T convertToObject(String s); final class Default implements BidiStringConverter, Serializable { @SuppressWarnings("rawtypes") public static final BidiStringConverter INSTANCE = new Default(); @SuppressWarnings("unchecked") public static BidiStringConverter instance() { return INSTANCE; } // ensure single instance private Default() { super(); } // simply return the object's #toString() result public String convertToString(S o) { return (o == null) ? null : o.toString(); } // simply return the string @SuppressWarnings("unchecked") public S convertToObject(String s) { return (S) s; } @Override public String toString() { return StringTools.buildSingletonToString(this); } private static final long serialVersionUID = 1L; private Object readResolve() { // replace this object with the singleton return INSTANCE; } } final class Disabled implements BidiStringConverter, Serializable { @SuppressWarnings("rawtypes") public static final BidiStringConverter INSTANCE = new Disabled(); @SuppressWarnings("unchecked") public static BidiStringConverter instance() { return INSTANCE; } // ensure single instance private Disabled() { super(); } // throw an exception public String convertToString(S o) { throw new UnsupportedOperationException(); } // throw an exception public S convertToObject(String s) { throw new UnsupportedOperationException(); } @Override public String toString() { return StringTools.buildSingletonToString(this); } private static final long serialVersionUID = 1L; private Object readResolve() { // replace this object with the singleton return INSTANCE; } } final class BooleanConverter implements BidiStringConverter, Serializable { public static final BidiStringConverter INSTANCE = new BooleanConverter(); public static BidiStringConverter instance() { return INSTANCE; } // ensure single instance private BooleanConverter() { super(); } /** Return "true" if the Boolean is true, otherwise return "false". */ public String convertToString(Boolean b) { return (b == null) ? null : b.toString(); } /** Return Boolean.TRUE if the string is "true" (case-insensitive), otherwise return Boolean.FALSE. */ public Boolean convertToObject(String s) { return (s == null) ? null : Boolean.valueOf(s); } @Override public String toString() { return StringTools.buildSingletonToString(this); } private static final long serialVersionUID = 1L; private Object readResolve() { // replace this object with the singleton return INSTANCE; } } final class IntegerConverter implements BidiStringConverter, Serializable { public static final BidiStringConverter INSTANCE = new IntegerConverter(); public static BidiStringConverter instance() { return INSTANCE; } // ensure single instance private IntegerConverter() { super(); } /** Integer's #toString() works well. */ public String convertToString(Integer integer) { return (integer == null) ? null : integer.toString(); } /** Convert the string to an Integer, if possible. */ public Integer convertToObject(String s) { return (s == null) ? null : Integer.valueOf(s); } @Override public String toString() { return StringTools.buildSingletonToString(this); } private static final long serialVersionUID = 1L; private Object readResolve() { // replace this object with the singleton return INSTANCE; } } }