-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataSet.java
168 lines (158 loc) · 5.55 KB
/
DataSet.java
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
package project2;
import java.net.URL;
import java.util.ArrayList;
/**
* This class represents the object DataSet. The object DataSet has the following
* properties: date, title, description, URL, and hattips.
*/
public class DataSet implements Comparable<DataSet> {
// store properties here
private Date date;
String title;
String description;
ArrayList<URL> links = new ArrayList<URL>();
private String hattips = "";
/**
* Constructs a new DataSet object with specified title, description and links.
* @param t is the title of the data
* @param d is the description of the data
* @param l is an array of URLs from the data
* @throws IllegalArgumentException if any of the parameters for title, description,
* and URL are invalid
*/
public DataSet(String t, String d, ArrayList<URL> l) throws IllegalArgumentException {
if (t == null)
throw new IllegalArgumentException("Title is null.");
else if (t.equals(""))
throw new IllegalArgumentException("Title is empty.");
else if (l == null)
throw new IllegalArgumentException("List is null.");
else if (l.size() == 0)
throw new IllegalArgumentException("List is empty.");
else if (d == null)
throw new IllegalArgumentException("Description is null.");
else if (d.equals(""))
throw new IllegalArgumentException("Description is empty.");
else {
title = t;
description = d;
links = l;
}
}
/**
* Sets the value of the date.
* @param newdate provides the date input
* @throws IllegalArgumentException checks to see if date is invalid based on when it is,
* or if the value is null/invalid
*/
public void setDate(Date newdate) throws IllegalArgumentException {
if (newdate == null)
throw new IllegalArgumentException("Date cannot be null");
else if (newdate.year < 2000)
throw new IllegalArgumentException("Date must be greater than or equal to 2000");
else if (newdate.toString().equals(""))
throw new IllegalArgumentException("Date is empty.");
else
this.date = newdate;
}
/**
* Returns the date of this object.
* @return the date of this DataSet object
*/
public Date getDate() {
return date;
}
/**
* Sets the value for hattips. Checks to see if value exists, or makes it empty.
* @param newhattips is the hattip value to be used for the hattip.
*/
public void setHatTips(String newhattips) {
if (newhattips == null)
this.hattips = "";
else if (newhattips.equals(""))
this.hattips = "";
else
this.hattips = newhattips;
}
/**
* Returns the hattips of the DataSet object.
* @return the hattips of the DataSet object.
*/
public String getHatTips() {
return hattips;
}
/**
* Determines which object is bigger based on date and if they are the same/missing,
* then by title. The comparison with title is case insensitve.
* @return an int, 0 if the same, > 0 if bigger, < 0 if smaller
*/
@Override
public int compareTo(DataSet o) {
// compared the titles equals ignore case if titles were equal
if (this.title.equalsIgnoreCase(o.title)) {
// if dates are there
if (this.date == null || o.date == null) {
return this.title.toLowerCase().compareTo(o.title.toLowerCase());
} else {
// compare dates
return this.date.compareTo(o.date);
}
// if not there return 0
}
return this.title.toLowerCase().compareTo(o.title.toLowerCase());
// return compare titles
}
/**
* Indicates whether or not two objects are the same based on dates
* and titles (note, not case sensitive).
* @return a boolean value, true if the same, false if different
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof DataSet))
return false;
DataSet other = (DataSet) obj;
if (this.date == null || other.date == null) {
if (this.title.equalsIgnoreCase(other.title)) {
return true;
}
return false;
}
if (this.date.equals(other.date))
if (this.title.equalsIgnoreCase(other.title))
return true;
else if (this.date == null) {
if (other.date != null)
return false;
} else
return false;
else
return false;
return true;
}
/**
* Returns a string representation of this object which includes the name,
* description, links, and date if it exists.
* @return the string representation of DataSet
*/
@Override
public String toString() {
StringBuilder strung = new StringBuilder();
try {
for (URL link : links) {
strung.append(link);
strung.append("\n");
}
} catch (NullPointerException ex) {
System.err.println("Error with links");
}
if (date == null)
return title + "\n" + description + "\n" + strung.toString();
else
return date.toString() + "\n" + title + "\n" + description + "\n" + strung.toString();
}
}