comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToCharacterConverter.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
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
1 /*
2 * Copyright cast(C) 2005 db4objects Inc. http://www.db4o.com
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * db4objects - Initial API and implementation
11 * Matt Carter - Improved primitive conversion support (bug 197679)
12 */
13 module org.eclipse.core.internal.databinding.conversion.StringToCharacterConverter;
14
15 import java.lang.all;
16
17 import org.eclipse.core.databinding.conversion.IConverter;
18
19 /**
20 * StringToCharacterConverter.
21 */
22 public class StringToCharacterConverter : IConverter {
23
24 private final bool primitiveTarget;
25
26 /**
27 *
28 * @param primitiveTarget
29 */
30 public this(bool primitiveTarget) {
31 this.primitiveTarget = primitiveTarget;
32 }
33
34 /*
35 * (non-Javadoc)
36 *
37 * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object)
38 */
39 public Object convert(Object source) {
40 if (source !is null && !( null !is cast(String)source ))
41 throw new IllegalArgumentException(
42 "String2Character: Expected type String, got type [" + source.getClass().getName() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
43
44 String s = cast(String) source;
45 if (source is null || s.equals("")) { //$NON-NLS-1$
46 if (primitiveTarget)
47 throw new IllegalArgumentException(
48 "String2Character: cannot convert null/empty string to character primitive"); //$NON-NLS-1$
49 return null;
50 }
51 Character result;
52
53 if (s.length() > 1)
54 throw new IllegalArgumentException(
55 "String2Character: string too long: " + s); //$NON-NLS-1$
56
57 try {
58 result = new Character(s.charAt(0));
59 } catch (Exception e) {
60 throw new IllegalArgumentException(
61 "String2Character: " + e.getMessage() + ": " + s); //$NON-NLS-1$ //$NON-NLS-2$
62 }
63
64 return result;
65 }
66
67 public Object getFromType() {
68 return String.classinfo;
69 }
70
71 public Object getToType() {
72 return primitiveTarget ? Character.TYPE : Character.classinfo;
73 }
74
75 /**
76 * @param primitive
77 * @return converter
78 */
79 public static StringToCharacterConverter toCharacter(bool primitive) {
80 return new StringToCharacterConverter(primitive);
81 }
82
83 }