-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexEntry.java
More file actions
118 lines (101 loc) · 2.92 KB
/
Copy pathIndexEntry.java
File metadata and controls
118 lines (101 loc) · 2.92 KB
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
package proj5;
import java.util.StringJoiner;
/**
* The IndexEntry models an entry into the index which contains a word and a page list
*/
public class IndexEntry implements Comparable<IndexEntry> {
private String word; // the word in the index
private int[] pages; //the pages that are associated with the word -> -1 meaning no page
private int numPages; //represents the current number of pages in the list
private final static int MAX_PAGES = 4;
/**
* Default constructor
* @param word
*/
public IndexEntry(String word) {
this.word = word;
pages = new int[MAX_PAGES];
numPages = 0;
for(int i = 0; i < pages.length; i++) {
pages[i] = -1;
}
}
/**
* Add a page to the page numbers for the word
*
* @param page the page number to add
* @return true if the page was added, false if this was the fifth entry (not added)
*/
public boolean addPage(int page) {
if(numPages == MAX_PAGES) {
return false;
} else {
if(!this.containsPage(page)) {
pages[numPages] = page;
numPages++;
}
return true;
}
}
/**
* Given a page, see if it is already in the page list
*
* @param page the page to see if it is in page list
* @return true if in page list, false otherwise
*/
public boolean containsPage(int page) {
for(int p : pages) {
if(p == page) {
return true;
}
}
return false;
}
/**
* Get a copy of the word
* @return a copy of the word in the index entry
*/
public String getWord() {
return new String(this.word);
}
/**
* Get the number of pages in the page list
* @return int representing # of pages
*/
public int getNumPages() {
return this.numPages;
}
/**
* Compare one index entry to another
* @param o the other object to compare
* @return value less than 0 if this is less than o, 0 if equal, greater than 0 if this is greater than o
*/
@Override
public int compareTo(IndexEntry o) {
return this.word.compareTo(o.word);
}
/**
* default toString
* @return a stringified version of an index entry
*/
@Override
public String toString() {
StringJoiner s = new StringJoiner(", ");
for(int i = 0; i < numPages; i++) {
s.add(String.valueOf(pages[i]));
}
return this.word + " {" + s + "}";
}
/**
* Compare two entries for equality
* @param o the other entry to compare
* @return true of this and other are equal, false otherwise
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof IndexEntry)) {
return false;
}
return this.word.compareTo(((IndexEntry) o).word) == 0;
}
}