-
Notifications
You must be signed in to change notification settings - Fork 4
/
Calendar.java
352 lines (315 loc) · 9.07 KB
/
Calendar.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.text.DecimalFormat;
public class Calendar
{
//filename target must be in same working directory as java program
private String fileName = "outputcalendar.ics";
private String HEADER = "BEGIN:VCALENDAR\nVERSION:2.0\n";
private String FOOTER = "END:VCALENDAR\n";
ArrayList<Vevent> allVevents = new ArrayList<Vevent>();
//default constructor
public Calendar()
{
}
//overloaded constructor
public Calendar(String fileName)
{
this.fileName = fileName;
importIcs(fileName);
}
//returns the filename
public String getFileName()
{
return fileName;
}
//imports a .ics file
private void importIcs(String inputIcsFile)
{
try
{
DataInputStream in = new DataInputStream(new FileInputStream(inputIcsFile));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
Vevent vevent = new Vevent();
String strLine;
int lineCount = 0;
String temp;
while ((strLine = br.readLine()) != null)
{
lineCount++;
//if a new event is detected
if (strLine.equals("BEGIN:VEVENT"))
{
vevent = new Vevent();
}
//if the end of the event details
else if(strLine.equals("END:VEVENT"))
{
allVevents.add(vevent);
}
//if UID detected
else if((temp = strLine.substring(0, 4)).equals("UID:"))
{
String temp2 = strLine.substring(4, strLine.length());
//if valid according to validation method in vevent
if(vevent.validUID(temp2))
{
vevent.setUID(temp2);
}
else
{
System.err.println("Invalid UID found at line " + lineCount + " in " + inputIcsFile);
}
}
//if DTSTAMP detected
else if((temp = strLine.substring(0, 8)).equals("DTSTAMP:"))
{
String temp2 = strLine.substring(8, strLine.length());
//if valid according to validation method in vevent
if(vevent.validDTSTAMP(temp2))
{
vevent.setDTSTAMP(temp2);
}
else
{
System.err.println("Invalid DTSTAMP found at line " + lineCount + " in " + inputIcsFile);
}
}
//if DTSTART detected
else if((temp = strLine.substring(0, 8)).equals("DTSTART:"))
{
String temp2 = strLine.substring(8, strLine.length());
//if valid according to validation method in vevent
if(vevent.validDTSTART(temp2))
{
vevent.setDTSTART(temp2);
}
else
{
System.err.println("Invalid DTSTART found at line " + lineCount + " in " + inputIcsFile);
}
}
//if DTEND detected
else if((temp = strLine.substring(0, 6)).equals("DTEND:"))
{
String temp2 = strLine.substring(6, strLine.length());
//if valid according to validation method in vevent
if(vevent.validDTEND(temp2))
{
vevent.setDTEND(temp2);
}
else
{
System.err.println("Invalid DTEND found at line " + lineCount + " in " + inputIcsFile);
}
}
//if SUMMARY detected
else if((temp = strLine.substring(0, 8)).equals("SUMMARY:"))
{
String temp2 = strLine.substring(8, strLine.length());
//if valid according to validation method in vevent
if(vevent.validSUMMARY(temp2))
{
vevent.setSUMMARY(temp2);
}
else
{
System.err.println("Invalid SUMMARY found at line " + lineCount + " in " + inputIcsFile);
}
}
//if ORGANIZER detected
else if((temp = strLine.substring(0, 10)).equals("ORGANIZER:"))
{
String temp2 = strLine.substring(10, strLine.length());
//if valid according to validation method in vevent
if(vevent.validORGANIZER(temp2))
{
vevent.setORGANIZER(temp2);
}
else
{
System.err.println("Invalid ORGANIZER found at line " + lineCount + " in " + inputIcsFile);
}
}
//if GEO detected
else if((temp = strLine.substring(0, 4)).equals("GEO:"))
{
String temp2 = strLine.substring(4, strLine.length());
//if valid according to validation method in vevent
if(vevent.validGEO(temp2))
{
vevent.setGEO(temp2);
}
else
{
System.err.println("Invalid GEO found at line " + lineCount + " in " + inputIcsFile);
}
}
//if CLASS detected
else if((temp = strLine.substring(0, 6)).equals("CLASS:"))
{
String temp2 = strLine.substring(6, strLine.length());
//if valid according to validation method in vevent
if(vevent.validCLASS(temp2))
{
vevent.setCLASS(temp2);
}
else
{
System.err.println("Invalid CLASS found at line " + lineCount + " in " + inputIcsFile);
}
}
//if COMMENT detected
else if((temp = strLine.substring(0, 8)).equals("COMMENT:"))
{
String temp2 = strLine.substring(8, strLine.length());
//if valid according to validation method in vevent
if(vevent.validCOMMENT(temp2))
{
vevent.setCOMMENT(temp2);
}
else
{
System.err.println("Invalid COMMENT found at line " + lineCount + " in " + inputIcsFile);
}
}
}
in.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
public void exportIcs()
{
try
{
FileWriter fstream = new FileWriter(fileName);
BufferedWriter out = new BufferedWriter(fstream);
//HEADER
out.write(HEADER);
//VEVENTS
Iterator veventsItr = allVevents.iterator();
while(veventsItr.hasNext())
{
out.write(veventsItr.next().toString());
}
//FOOTER
out.write(FOOTER);
out.close();
}
catch (Exception e)
{
System.err.println("Error when trying to export to .ics file:\n" + e.getMessage());
}
}
public void printAllEvents()
{
Iterator veventsItr = allVevents.iterator();
System.out.print("-----------------------------------\n");
while(veventsItr.hasNext())
{
System.out.print(veventsItr.next().toString());
System.out.println("-----------------------------------");
}
}
public void addEvent(Vevent inputVevent)
{
try
{
if (inputVevent.isValid())
{
allVevents.add(inputVevent);
}
else
{
System.err.println("The event that you are trying to add is invalid!");
}
}
catch(NullPointerException e)
{
System.out.println("you are trying to input an event with null values!");
}
}
//sorts the events in the calendar by the start date
public void sortCalendar()
{
Collections.sort(allVevents);
}
//prints the results of the great circle distance
public void printGreatCircleDistance()
{
//ensures that there are at least 2 events in the calendar before calculating the GCD
if (allVevents.size() < 2)
{
System.err.println("At least 2 events are needed to calculate GCD");
return;
}
Iterator<Vevent> veventsItr = allVevents.iterator();
Vevent iter = veventsItr.next();
Vevent iterPrev = iter;
DecimalFormat df = new DecimalFormat("#,###.##");
System.out.print("-----------------------------------\n");
System.out.print("------GREAT-CIRCLE-DISTANCE--------\n\n");
Float curLat = iter.getLat();
Float curLon = iter.getLon();
String currSum = iter.getSUMMARY();
while(veventsItr.hasNext())
{
//move the first iterator forward
iter = veventsItr.next();
//if the geo of the current iteration is not empty
if(iter.getGEO() != null)
{
float nextLat = iter.getLat();
float nextLon = iter.getLon();
//calculate GCD and store it in comment section of first event
double gcd = greatCircleDistanceBetween(curLat, curLon, nextLat, nextLon);
iterPrev.setCOMMENT("The Great Circle Distance to the next event(" + shortenString(iter.getSUMMARY()) + "...) is " + df.format(gcd)+ " miles (" + df.format(1.609344 * gcd) + "km)");
//print gcd for first event to the next event
System.out.println(currSum + " to " + iter.getSUMMARY() + " is " + df.format(gcd) + " miles (" + df.format(1.609344 * gcd) + "km)\n");
//updates values
curLat = iter.getLat();
curLon = iter.getLon();
currSum = iter.getSUMMARY();
iterPrev = iter;
}
else
{
System.out.println(iter.getSUMMARY() + " does not have a GEO, GCD will not take this event into consideration....");
}
System.out.println("-----------------------------------\n");
}
}
//returns the first x characters of a string if the string is considered to be too long
private String shortenString(String input)
{
String result = "";
int RETURNLENGTH = 7;
if (input.length() <= RETURNLENGTH)
{
return input;
}
for (int i = 0; i < RETURNLENGTH; i++)
{
result += input.charAt(i);
}
return result;
}
//calculates the great circle distance between two locations in statue miles
public double greatCircleDistanceBetween(float srcLat, float srcLon, float dstLat, float dstLon)
{
double earthRadius = 3958.75; // miles (or 6371.0 kilometers)
double dLat = Math.toRadians(dstLat- srcLat);
double dLng = Math.toRadians(dstLon - srcLon);
double sin_dLat = Math.sin(dLat / 2);
double sin_dLng = Math.sin(dLng / 2);
double a = Math.pow(sin_dLat, 2) + Math.pow(sin_dLng, 2) * Math.cos(Math.toRadians(srcLat)) * Math.cos(Math.toRadians(dstLat));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
return dist;
}
}