Skip to content

Commit 40ecf8f

Browse files
jphughes88fmbenhassine
authored andcommitted
Implement equals and hashcode in CompletionProposal
1 parent efd8629 commit 40ecf8f

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

spring-shell-core/src/main/java/org/springframework/shell/CompletionProposal.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 the original author or authors.
2+
* Copyright 2016-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.shell;
1818

19+
import java.util.Objects;
20+
1921
/**
2022
* Represents a proposal for TAB completion, made not only of the text to append, but also metadata about the proposal.
2123
*
@@ -118,4 +120,21 @@ public boolean dontQuote() {
118120
public String toString() {
119121
return value;
120122
}
123+
124+
@Override
125+
public boolean equals(Object o) {
126+
if (this == o) return true;
127+
if (o == null || getClass() != o.getClass()) return false;
128+
CompletionProposal that = (CompletionProposal) o;
129+
return dontQuote == that.dontQuote &&
130+
Objects.equals(value, that.value) &&
131+
Objects.equals(displayText, that.displayText) &&
132+
Objects.equals(description, that.description) &&
133+
Objects.equals(category, that.category);
134+
}
135+
136+
@Override
137+
public int hashCode() {
138+
return Objects.hash(value, displayText, description, category, dontQuote);
139+
}
121140
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2018-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell;
17+
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
public class CompletionProposalTest {
24+
25+
private final String PROPOSAL = "test proposal";
26+
private final String DESCRIPTION = "description";
27+
private final String DISPLAY_TEXT = "displayText";
28+
private final boolean DONT_QUOTE = true;
29+
private final String CATEGORY = "category";
30+
CompletionProposal completionProposal;
31+
32+
33+
@BeforeEach
34+
public void setup() {
35+
completionProposal = new CompletionProposal(PROPOSAL);
36+
completionProposal.category(CATEGORY);
37+
completionProposal.description(DESCRIPTION);
38+
completionProposal.displayText(DISPLAY_TEXT);
39+
completionProposal.dontQuote(DONT_QUOTE);
40+
}
41+
42+
@Test
43+
public void equals() {
44+
CompletionProposal completionProposal2 = new CompletionProposal(PROPOSAL);
45+
completionProposal2.category(CATEGORY);
46+
completionProposal2.description(DESCRIPTION);
47+
completionProposal2.displayText(DISPLAY_TEXT);
48+
completionProposal2.dontQuote(DONT_QUOTE);
49+
50+
assertEquals(completionProposal, completionProposal2);
51+
}
52+
53+
@Test
54+
public void hashcode() {
55+
CompletionProposal completionProposal2 = new CompletionProposal(PROPOSAL);
56+
completionProposal2.category(CATEGORY);
57+
completionProposal2.description(DESCRIPTION);
58+
completionProposal2.displayText(DISPLAY_TEXT);
59+
completionProposal2.dontQuote(DONT_QUOTE);
60+
assertEquals(completionProposal.hashCode(), completionProposal2.hashCode());
61+
}
62+
}

0 commit comments

Comments
 (0)