Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToCharacterConverter.d @ 88:9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:10:09 +0200 |
parents | 6be48cf9f95c |
children |
rev | line source |
---|---|
78 | 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) { | |
85 | 40 if (source !is null && !( null !is cast(ArrayWrapperString)source )) |
78 | 41 throw new IllegalArgumentException( |
85 | 42 Format("String2Character: Expected type String, got type [{}]", source.classinfo.name) ); //$NON-NLS-1$ //$NON-NLS-2$ |
78 | 43 |
85 | 44 String s = stringcast(source); |
78 | 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( | |
85 | 55 "String2Character: string too long: " ~ s); //$NON-NLS-1$ |
78 | 56 |
57 try { | |
58 result = new Character(s.charAt(0)); | |
59 } catch (Exception e) { | |
60 throw new IllegalArgumentException( | |
85 | 61 Format("String2Character: {}: {}", e.msg, s)); //$NON-NLS-1$ //$NON-NLS-2$ |
78 | 62 } |
63 | |
64 return result; | |
65 } | |
66 | |
67 public Object getFromType() { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
68 return Class.fromType!(StringCls); |
78 | 69 } |
70 | |
71 public Object getToType() { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
72 return primitiveTarget ? Character.TYPE : Class.fromType!(Character); |
78 | 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 } |