| /******************************************************************************* |
| * Copyright (c) 2007, 2012 Oracle and/or its affiliates. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| * which accompanies this distribution. |
| * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| * and the Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * Oracle - initial API and implementation |
| * |
| ******************************************************************************/ |
| package org.eclipse.persistence.tools.utility; |
| |
| 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<T> extends StringConverter<T> { |
| |
| /** |
| * Converts 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 |
| * <code>null</code>, <code>null</code> is returned. |
| */ |
| T convertToObject(String value); |
| |
| final class BooleanConverter implements BidiStringConverter<Boolean>, Serializable { |
| public static final BidiStringConverter<Boolean> INSTANCE = new BooleanConverter(); |
| private static final long serialVersionUID = 1L; |
| // ensure single instance |
| private BooleanConverter() { |
| super(); |
| } |
| public static BidiStringConverter<Boolean> instance() { |
| return INSTANCE; |
| } |
| /** * Returns Boolean.FALSE. */ |
| @Override |
| public Boolean convertToObject(String s) { |
| return (s == null) ? null : Boolean.valueOf(s); |
| } |
| /** * Returns "false". */ |
| @Override |
| public String convertToString(Boolean b) { |
| return (b == null) ? null : b.toString(); |
| } |
| private Object readResolve() { |
| // replace this object with the singleton |
| return INSTANCE; |
| } |
| @Override |
| public String toString() { |
| return StringTools.buildSingletonToString(this); |
| } |
| } |
| |
| final class Default<S> implements BidiStringConverter<S>, Serializable { |
| @SuppressWarnings("rawtypes") |
| public static final BidiStringConverter INSTANCE = new Default(); |
| private static final long serialVersionUID = 1L; |
| // ensure single instance |
| private Default() { |
| super(); |
| } |
| @SuppressWarnings("unchecked") |
| public static <R> BidiStringConverter<R> instance() { |
| return INSTANCE; |
| } |
| // * Returns the string |
| @Override |
| @SuppressWarnings("unchecked") |
| public S convertToObject(String s) { |
| return (S) s; |
| } |
| // * Returns the object's #toString() result |
| @Override |
| public String convertToString(S o) { |
| return (o == null) ? null : o.toString(); |
| } |
| private Object readResolve() { |
| // replace this object with the singleton |
| return INSTANCE; |
| } |
| @Override |
| public String toString() { |
| return StringTools.buildSingletonToString(this); |
| } |
| } |
| |
| final class Disabled<S> implements BidiStringConverter<S>, Serializable { |
| @SuppressWarnings("rawtypes") |
| public static final BidiStringConverter INSTANCE = new Disabled(); |
| private static final long serialVersionUID = 1L; |
| // ensure single instance |
| private Disabled() { |
| super(); |
| } |
| @SuppressWarnings("unchecked") |
| public static <R> BidiStringConverter<R> instance() { |
| return INSTANCE; |
| } |
| // throw an exception |
| @Override |
| public S convertToObject(String s) { |
| throw new UnsupportedOperationException(); |
| } |
| // throw an exception |
| @Override |
| public String convertToString(S o) { |
| throw new UnsupportedOperationException(); |
| } |
| private Object readResolve() { |
| // replace this object with the singleton |
| return INSTANCE; |
| } |
| @Override |
| public String toString() { |
| return StringTools.buildSingletonToString(this); |
| } |
| } |
| |
| final class IntegerConverter implements BidiStringConverter<Integer>, Serializable { |
| public static final BidiStringConverter<Integer> INSTANCE = new IntegerConverter(); |
| private static final long serialVersionUID = 1L; |
| // ensure single instance |
| private IntegerConverter() { |
| super(); |
| } |
| public static BidiStringConverter<Integer> instance() { |
| return INSTANCE; |
| } |
| /** Convert the string to an Integer, if possible. */ |
| @Override |
| public Integer convertToObject(String s) { |
| return (s == null) ? null : Integer.valueOf(s); |
| } |
| /** Integer's #toString() works well. */ |
| @Override |
| public String convertToString(Integer integer) { |
| return (integer == null) ? null : integer.toString(); |
| } |
| private Object readResolve() { |
| // replace this object with the singleton |
| return INSTANCE; |
| } |
| @Override |
| public String toString() { |
| return StringTools.buildSingletonToString(this); |
| } |
| } |
| } |