Skip to main content
summaryrefslogtreecommitdiffstats
blob: 919def901de995759d9afc6f452de554c3a33cf8 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/********************************************************************************
 * Copyright (c) 2015-2018 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 ********************************************************************************/


package org.eclipse.mdm.api.base.query;

import java.util.Objects;

/**
 * Filter item contains either a {@link Condition} or an {@link BooleanOperator}.
 *
 * @since 1.0.0
 * @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
 * @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
 */
public final class FilterItem {

	// ======================================================================
	// Class variables
	// ======================================================================

	/**
	 * Contains the {@link BooleanOperator#AND}.
	 */
	static final FilterItem AND = new FilterItem(BooleanOperator.AND);

	/**
	 * Contains the {@link BooleanOperator#OR}.
	 */
	static final FilterItem OR = new FilterItem(BooleanOperator.OR);

	/**
	 * Contains the {@link BooleanOperator#NOT}.
	 */
	static final FilterItem NOT = new FilterItem(BooleanOperator.NOT);

	/**
	 * Contains the {@link BooleanOperator#OPEN}.
	 */
	static final FilterItem OPEN = new FilterItem(BracketOperator.OPEN);

	/**
	 * Contains the {@link BooleanOperator#CLOSE}.
	 */
	static final FilterItem CLOSE = new FilterItem(BracketOperator.CLOSE);

	// ======================================================================
	// Instance variables
	// ======================================================================

	private final Condition condition;
	private final BooleanOperator booleanOperator;
	private final BracketOperator bracketOperator;

	// ======================================================================
	// Constructors
	// ======================================================================

	/**
	 * Constructor.
	 *
	 * @param condition
	 *            The {@link Condition}.
	 */
	FilterItem(Condition condition) {
		this.condition = condition;
		this.bracketOperator = null;
		this.booleanOperator = null;
	}

	/**
	 * Constructor.
	 *
	 * @param booleanOperator
	 *            The {@link BooleanOperator}.
	 */
	private FilterItem(BooleanOperator booleanOperator) {
		this.condition = null;
		this.bracketOperator = null;
		this.booleanOperator = booleanOperator;
	}
	
	/**
	 * Constructor.
	 *
	 * @param bracketoperator
	 *            The {@link BracketOperator}.
	 */
	private FilterItem(BracketOperator bracketoperator) {
		this.condition = null;
		this.booleanOperator = null;
		this.bracketOperator = bracketoperator;
	}

	// ======================================================================
	// Public methods
	// ======================================================================

	/**
	 * Checks whether an {@link BooleanOperator} is contained.
	 *
	 * @return True if an {@code BooleanOperator} is contained.
	 */
	public boolean isBooleanOperator() {
		return booleanOperator != null;
	}
	

	/**
	 * Returns the contained {@link BooleanOperator}.
	 *
	 * @return The {@code BooleanOperator} is returned.
	 * @throws IllegalStateException
	 *             Thrown if {@code BooleanOperator} is not contained.
	 */
	public BooleanOperator getBooleanOperator() {
		if (isBooleanOperator()) {
			return booleanOperator;
		}

		throw new IllegalStateException("Item does not contain an booleanOperator.");
	}

	/**
	 * Checks whether an {@link Condition} is contained.
	 *
	 * @return True if an {@code Condition} is contained.
	 */
	public boolean isCondition() {
		return condition != null;
	}

	/**
	 * Returns the contained {@link Condition}.
	 *
	 * @return The {@code Condition} is returned.
	 * @throws IllegalStateException
	 *             Thrown if {@code Condition} is not contained.
	 */
	public Condition getCondition() {
		if (isCondition()) {
			return condition;
		}

		throw new IllegalStateException("Item does not contain a condition.");
	}
	
	/**
	 * Checks whether an {@link BrackerOperator} is contained.
	 *
	 * @return True if an {@code BracketOperator} is contained.
	 */
	public boolean isBracketOperator() {
		return bracketOperator != null;
	}
	
	/**
	 * Returns the contained {@link Condition}.
	 *
	 * @return The {@code Condition} is returned.
	 * @throws IllegalStateException
	 *             Thrown if {@code Condition} is not contained.
	 */
	public BracketOperator getBracketOperator() {
		if (isBracketOperator()) {
			return bracketOperator;
		}

		throw new IllegalStateException("Item does not contain a condition.");
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object other) {
		if (other == this) {
			return true;
		}
		if (!(other instanceof FilterItem)) {
			return false;
		}

		FilterItem filterItem = (FilterItem) other;

		return Objects.equals(this.condition, filterItem.condition)
				&& Objects.equals(this.booleanOperator, filterItem.booleanOperator)
				&& Objects.equals(this.bracketOperator, filterItem.bracketOperator);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return Objects.hash(condition, booleanOperator, bracketOperator);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		if (isBooleanOperator()) {
			return " " + booleanOperator.toString() + " ";
		} else if (isBracketOperator()) {
			return bracketOperator.toString();
		} else {
			return condition.toString();
		}
	}

}

Back to the top