78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007 Matt Carter and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * Matt Carter - initial API and implementation
|
|
10 ******************************************************************************/
|
|
11
|
|
12 module org.eclipse.core.internal.databinding.conversion.CharacterToStringConverter;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import org.eclipse.core.databinding.conversion.Converter;
|
|
17
|
|
18 /**
|
|
19 * Converts a character to a string.
|
|
20 */
|
|
21 public class CharacterToStringConverter : Converter {
|
|
22 private final bool primitive;
|
|
23
|
|
24 /**
|
|
25 * @param primitive
|
|
26 */
|
|
27 private this(bool primitive) {
|
|
28 super(primitive ? Character.TYPE : Character.classinfo, String.classinfo);
|
|
29 this.primitive = primitive;
|
|
30 }
|
|
31
|
|
32 /*
|
|
33 * (non-Javadoc)
|
|
34 *
|
|
35 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
|
|
36 */
|
|
37 public Object convert(Object fromObject) {
|
|
38 // Null is allowed when the type is not primitive.
|
|
39 if (fromObject is null) {
|
|
40 if (primitive)
|
|
41 throw new IllegalArgumentException(
|
|
42 "'fromObject' is null. Cannot convert to primitive char."); //$NON-NLS-1$
|
|
43 return ""; //$NON-NLS-1$
|
|
44 }
|
|
45
|
|
46 if (!( null !is cast(Character)fromObject )) {
|
|
47 throw new IllegalArgumentException(
|
|
48 "'fromObject' is not of type [Character]."); //$NON-NLS-1$
|
|
49 }
|
|
50
|
|
51 return String.valueOf((cast(Character) fromObject).charValue());
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * @param primitive
|
|
56 * @return converter
|
|
57 */
|
|
58 public static CharacterToStringConverter fromCharacter(bool primitive) {
|
|
59 return new CharacterToStringConverter(primitive);
|
|
60 }
|
|
61
|
|
62 }
|