Skip to main content
summaryrefslogtreecommitdiffstats
blob: d96b0413c610c25deae11178d100657680fb1f5a (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
/********************************************************************************
 * 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;

import org.eclipse.mdm.api.base.adapter.Attribute;
import org.eclipse.mdm.api.base.model.Value;

/**
 * Describes a filter condition.
 *
 * @since 1.0.0
 * @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
 * @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
 * @see Attribute
 * @see ComparisonOperator
 */
public final class Condition {

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

	private final Attribute attribute;
	private final Value value;
	private final ComparisonOperator comparisonOperator;

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

	/**
	 * Constructor.
	 *
	 * @param attribute
	 *            The {@link Attribute} this condition will be applied to.
	 * @param comparisonOperator
	 *            The condition {@link ComparisonOperator}.
	 * @param unit
	 *            The unit of the created value.
	 * @param input
	 *            The condition value.
	 */
	Condition(Attribute attribute, ComparisonOperator comparisonOperator, String unit, Object input) {
		this.attribute = attribute;
		this.comparisonOperator = comparisonOperator;
		value = comparisonOperator.requiresSequence() ? attribute.createValueSeq(unit, input)
				: attribute.createValue(unit, input);
	}

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

	/**
	 * Returns the target {@link Attribute}.
	 *
	 * @return The target {@code Attribute} is returned.
	 */
	public Attribute getAttribute() {
		return attribute;
	}

	/**
	 * Returns the condition {@link Value}.
	 *
	 * @return The condition {@code Value} is returned.
	 */
	public Value getValue() {
		return value;
	}

	/**
	 * Returns the condition {@link ComparisonOperator}.
	 *
	 * @return The condition {@code ComparisonOperator} is returned.
	 */
	public ComparisonOperator getComparisonOperator() {
		return comparisonOperator;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object other) {
		if (other == this) {
			return true;
		}
		if (!(other instanceof Condition)) {
			return false;
		}

		Condition condition = (Condition) other;

		return Objects.equals(this.attribute, condition.attribute)
				&& Objects.equals(this.value, condition.value)
				&& Objects.equals(this.comparisonOperator, condition.comparisonOperator);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return Objects.hash(attribute, value, comparisonOperator);
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return attribute.getEntityType().getName() + "." + attribute.getName() + " " + comparisonOperator + " " + value;
	}
}

Back to the top