Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d658a43e03f11f50f3394ee485c7967f0290836a (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright (c) 2009, 2017 Cloudsmith Inc. 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:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata.expression;

import java.util.*;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.metadata.VersionRange;
import org.eclipse.equinox.p2.metadata.expression.IEvaluationContext;
import org.eclipse.equinox.p2.metadata.expression.SimplePattern;
import org.osgi.framework.Filter;

/**
 * An expression that represents a constant value.
 */
public final class Literal extends Expression {
	public static final Literal FALSE_CONSTANT = new Literal(Boolean.FALSE);

	public static final Literal NULL_CONSTANT = new Literal(null);

	public static final Literal TRUE_CONSTANT = new Literal(Boolean.TRUE);

	public static Literal create(Object value) {
		if (value == null)
			return NULL_CONSTANT;
		if (value == Boolean.TRUE)
			return TRUE_CONSTANT;
		if (value == Boolean.FALSE)
			return FALSE_CONSTANT;
		return new Literal(value);
	}

	public final Object value;

	private Literal(Object value) {
		this.value = value;
	}

	@Override
	@SuppressWarnings({"unchecked", "rawtypes"})
	public int compareTo(Expression e) {
		int cmp = super.compareTo(e);
		if (cmp != 0)
			return cmp;

		Object eValue = ((Literal) e).value;
		if (value == null)
			return eValue == null ? 0 : -1;

		if (eValue == null)
			return 1;

		if (eValue.getClass() == value.getClass())
			return ((Comparable) value).compareTo(eValue);

		return eValue.getClass().getName().compareTo(value.getClass().getName());
	}

	@Override
	public boolean equals(Object o) {
		if (super.equals(o)) {
			Literal bo = (Literal) o;
			return value == null ? bo.value == null : value.equals(bo.value);
		}
		return false;
	}

	@Override
	public Object evaluate(IEvaluationContext context) {
		return value;
	}

	@Override
	public int getExpressionType() {
		return TYPE_LITERAL;
	}

	@Override
	public String getOperator() {
		return "<literal>"; //$NON-NLS-1$
	}

	@Override
	public int getPriority() {
		return PRIORITY_LITERAL;
	}

	@Override
	public int hashCode() {
		return 31 + value.hashCode();
	}

	@Override
	public void toLDAPString(StringBuffer buf) {
		if (!(value instanceof Filter))
			throw new UnsupportedOperationException();
		buf.append(value.toString());
	}

	@Override
	public void toString(StringBuffer bld, Variable rootVariable) {
		appendValue(bld, value);
	}

	private static void appendValue(StringBuffer bld, Object value) {
		if (value == null)
			bld.append("null"); //$NON-NLS-1$
		else if (value == Boolean.TRUE)
			bld.append("true"); //$NON-NLS-1$
		else if (value == Boolean.FALSE)
			bld.append("false"); //$NON-NLS-1$
		else if (value instanceof String)
			appendQuotedString(bld, (String) value);
		else if (value instanceof Number)
			bld.append(value.toString());
		else if (value instanceof SimplePattern) {
			appendEscaped(bld, '/', value.toString());
		} else if (value instanceof Version) {
			bld.append("version("); //$NON-NLS-1$
			appendQuotedString(bld, value.toString());
			bld.append(')');
		} else if (value instanceof VersionRange) {
			bld.append("range("); //$NON-NLS-1$
			appendQuotedString(bld, value.toString());
			bld.append(')');
		} else if (value instanceof Class<?>) {
			bld.append("class("); //$NON-NLS-1$
			appendQuotedString(bld, value.toString());
			bld.append(')');
		} else if (value instanceof Filter) {
			bld.append("filter("); //$NON-NLS-1$
			appendQuotedString(bld, value.toString());
			bld.append(')');
		} else if (value instanceof Set<?>) {
			bld.append("set("); //$NON-NLS-1$
			appendLiteralCollection(bld, (Collection<?>) value);
			bld.append(')');
		} else if (value instanceof Collection<?>)
			appendLiteralCollection(bld, (Collection<?>) value);
		else
			bld.append(value);

	}

	private static void appendLiteralCollection(StringBuffer bld, Collection<?> collection) {
		bld.append('[');
		Iterator<?> iter = collection.iterator();
		if (iter.hasNext()) {
			appendValue(bld, iter.next());
			while (iter.hasNext()) {
				bld.append(',');
				appendValue(bld, iter.next());
			}
		}
		bld.append(']');
	}

	private static void appendQuotedString(StringBuffer bld, String str) {
		if (str.indexOf('\'') < 0) {
			bld.append('\'');
			bld.append(str);
			bld.append('\'');
		} else
			appendEscaped(bld, '"', str);
	}

	private static void appendEscaped(StringBuffer bld, char delimiter, String str) {
		bld.append(delimiter);
		int top = str.length();
		for (int idx = 0; idx < top; ++idx) {
			char c = str.charAt(idx);
			if (c == delimiter || c == '\\')
				bld.append('\\');
			bld.append(c);
		}
		bld.append(delimiter);
	}
}

Back to the top