Skip to main content
summaryrefslogtreecommitdiffstats
blob: 66b49ff157ca9de4d35288e9c3c4186853de1a1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*******************************************************************************
 * Copyright (c) 2006, 2008 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.core.internal.utility.jdt;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jpt.common.core.utility.jdt.ExpressionConverter;
import org.eclipse.jpt.common.utility.internal.StringTools;

/**
 * Gather together the common implementation behavior.
 * T is the type of the object to be converted to and from an expression.
 * 
 * We're still figuring out Java Generics here.... The methods in this abstract
 * class work fine with any subclass of Expression E; but a ClassCastException
 * will occur as soon as we call any method implemented by a subclass
 * (e.g. StringExpressionConverter) that expects a particular subclass of
 * Expression (e.g. StringLiteral).
 */
public abstract class AbstractExpressionConverter<T>
	implements ExpressionConverter<T>
{

	protected AbstractExpressionConverter() {
		super();
	}


	// ********** object -> expression **********

	public Expression convert(T object, AST ast) {
		return (object == null) ? this.convertNull(ast) : this.convertObject(object, ast);
	}

	/**
	 * Return the expression for a null object. By default, a null object will
	 * be converted into a null expression.
	 */
	protected Expression convertNull(@SuppressWarnings("unused") AST ast) {
		return null;
	}

	/**
	 * The specified object is not null.
	 * @see #convert(Object, AST)
	 */
	protected abstract Expression convertObject(T object, AST ast);


	// ********** expression -> object **********

	public T convert(Expression expression) {
		return (expression == null) ? this.convertNull() : this.convertExpression(expression);
	}

	/**
	 * Return the object for a null expression. By default, a null expression will
	 * be converted into a null object.
	 */
	protected T convertNull() {
		return null;
	}

	/**
	 * The specified expression is not null.
	 * @see #convert(Expression)
	 */
	protected abstract T convertExpression(Expression expression);

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this);
	}

}

Back to the top