|
2 | 2 |
|
3 | 3 | import org.javawebstack.orm.TableInfo; |
4 | 4 |
|
5 | | -import java.util.LinkedList; |
6 | | -import java.util.stream.Collectors; |
| 5 | +import java.util.Objects; |
7 | 6 |
|
8 | 7 | /** |
9 | | - * The QueryOrderBy class serves as an aggregation of order by elements. It extends a list, because the order of the |
10 | | - * order by statements is of relevance. |
| 8 | + * The QueryOrderBy class encodes an Order By Statement. |
11 | 9 | */ |
12 | | -public class QueryOrderBy extends LinkedList<QueryOrderByElement>{ |
| 10 | +public class QueryOrderBy { |
| 11 | + |
| 12 | + private final QueryColumn queryColumn; |
| 13 | + private final boolean desc; |
| 14 | + |
| 15 | + QueryOrderBy(String columnName, boolean desc) { |
| 16 | + queryColumn = new QueryColumn(columnName); |
| 17 | + this.desc = desc; |
| 18 | + } |
| 19 | + |
| 20 | + QueryOrderBy(QueryColumn column, boolean desc) { |
| 21 | + this.queryColumn = column; |
| 22 | + this.desc = desc; |
| 23 | + } |
13 | 24 |
|
14 | 25 | /** |
15 | | - * Add a new order by statement. If a statement with the same column name already exists it will not add the |
16 | | - * statement. |
| 26 | + * Retrieves the QueryColumn of the statement which encodes the column name. |
17 | 27 | * |
18 | | - * @param columnName The column name to order by. |
19 | | - * @param desc If the column should be order descendingly. |
20 | | - * @return True if adding the statement was successful. False otherwise. |
| 28 | + * @return The encoding QueryColumn object. |
21 | 29 | */ |
22 | | - public boolean add(String columnName, boolean desc) { |
23 | | - return this.add(new QueryColumn(columnName), desc); |
| 30 | + public QueryColumn getQueryColumn() { |
| 31 | + return queryColumn; |
24 | 32 | } |
25 | 33 |
|
26 | 34 | /** |
27 | | - * Add a new order by statement. If a statement with the same column name already exists it will not add the |
28 | | - * statement. |
| 35 | + * Retrieves the information if this column is ordered ascendingly or descendingly. |
29 | 36 | * |
30 | | - * @param column The column to be ordered by. It will retrieve the name from the QueryColumn. |
31 | | - * @param desc If the column should be order descendingly. |
32 | | - * @return True if adding the statement was successful. False otherwise. |
| 37 | + * @return false if ascending, true if descending. |
33 | 38 | */ |
34 | | - public boolean add(QueryColumn column, boolean desc) { |
35 | | - return this.add(new QueryOrderByElement(column, desc)); |
| 39 | + public boolean isDesc() { |
| 40 | + return desc; |
36 | 41 | } |
37 | 42 |
|
38 | | - @Override |
39 | 43 | /** |
40 | | - * Add a new order by statement. If a statement with the same column name already exists it will not add the |
41 | | - * statement. |
| 44 | + * Compares the encoded column name. |
42 | 45 | * |
43 | | - * @param element The direct QueryOrderByElement which encodes the order by statement. |
44 | | - * @return True if adding the statement was successful. False otherwise. |
| 46 | + * @param o An object to compare to. |
| 47 | + * @return True if the object is a QueryOrderBy with a QueryColumn with generates the same identifier. |
45 | 48 | */ |
46 | | - public boolean add(QueryOrderByElement element) { |
47 | | - boolean hasBeenAdded = false; |
48 | | - if(!willOverwrite(element)) |
49 | | - hasBeenAdded = super.add(element); |
50 | | - |
51 | | - return hasBeenAdded; |
| 49 | + public boolean hasEqualColumn(Object o) { |
| 50 | + if (this == o) return true; |
| 51 | + if (o == null || getClass() != o.getClass()) return false; |
| 52 | + QueryOrderBy that = (QueryOrderBy) o; |
| 53 | + return getQueryColumn().equals(that.getQueryColumn()); |
52 | 54 | } |
53 | 55 |
|
54 | | - private boolean willOverwrite(QueryOrderByElement element) { |
55 | | - return this.stream().anyMatch(element::hasEqualColumn); |
| 56 | + public boolean equals(Object o) { |
| 57 | + if (this == o) return true; |
| 58 | + if (o == null || getClass() != o.getClass()) return false; |
| 59 | + QueryOrderBy that = (QueryOrderBy) o; |
| 60 | + return isDesc() == that.isDesc() && getQueryColumn().equals(that.getQueryColumn()); |
56 | 61 | } |
57 | 62 |
|
| 63 | + public int hashCode() { |
| 64 | + return Objects.hash(getQueryColumn(), isDesc()); |
| 65 | + } |
58 | 66 |
|
59 | | - // The toString methods are specific to MySQL so they might have to be replaced later on. |
60 | | - @Override |
61 | 67 | public String toString() { |
62 | | - return toString(null); |
| 68 | + return this.toString(null); |
63 | 69 | } |
64 | 70 |
|
65 | 71 | public String toString(TableInfo info) { |
66 | | - return this.stream() |
67 | | - .map(singleOrderByElement -> singleOrderByElement.toString(info)) |
68 | | - .collect(Collectors.joining(",")); |
| 72 | + String stringifiedOrderBy = getQueryColumn().toString(info); |
| 73 | + if (isDesc()) |
| 74 | + stringifiedOrderBy += " DESC"; |
| 75 | + return stringifiedOrderBy; |
69 | 76 | } |
| 77 | + |
70 | 78 | } |
0 commit comments