diff org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/CharacterToStringConverter.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/CharacterToStringConverter.d	Tue Apr 14 11:35:29 2009 +0200
@@ -0,0 +1,62 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Matt Carter and others.
+ * 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:
+ *     Matt Carter - initial API and implementation
+ ******************************************************************************/
+
+module org.eclipse.core.internal.databinding.conversion.CharacterToStringConverter;
+
+import java.lang.all;
+
+import org.eclipse.core.databinding.conversion.Converter;
+
+/**
+ * Converts a character to a string.
+ */
+public class CharacterToStringConverter : Converter {
+    private final bool primitive;
+
+    /**
+     * @param primitive
+     */
+    private this(bool primitive) {
+        super(primitive ? Character.TYPE : Character.classinfo, String.classinfo);
+        this.primitive = primitive;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
+     */
+    public Object convert(Object fromObject) {
+        // Null is allowed when the type is not primitive.
+        if (fromObject is null) {
+            if (primitive)
+                throw new IllegalArgumentException(
+                        "'fromObject' is null. Cannot convert to primitive char."); //$NON-NLS-1$
+            return ""; //$NON-NLS-1$
+        }
+
+        if (!( null !is cast(Character)fromObject )) {
+            throw new IllegalArgumentException(
+                    "'fromObject' is not of type [Character]."); //$NON-NLS-1$
+        }
+
+        return String.valueOf((cast(Character) fromObject).charValue());
+    }
+
+    /**
+     * @param primitive
+     * @return converter
+     */
+    public static CharacterToStringConverter fromCharacter(bool primitive) {
+        return new CharacterToStringConverter(primitive);
+    }
+
+}