-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreshwork.java
More file actions
258 lines (216 loc) · 8.3 KB
/
freshwork.java
File metadata and controls
258 lines (216 loc) · 8.3 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
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
package Test;
import java.util.*;
import java.io.*;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONTokener;
import org.json.JSONException;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.time.LocalTime;
@SuppressWarnings("serial")
class InvalidKey extends Exception {
}
@SuppressWarnings({ "serial", "unused" })
class DuplicateKey extends Exception {
}
@SuppressWarnings("serial")
class ValueSizeExceeded extends Exception{
}
@SuppressWarnings("serial")
class KeySizeExceeded extends Exception{
}
@SuppressWarnings("serial")
class TimeExceeded extends Exception {
}
public class DataFile {
private static Instrumentation instrument;
private final String DataPath; // Making DataPath immutable
DataFile(String pathway) throws JSONException {
DataPath = pathway;
JSONObject values = new JSONObject();
values.put(" ", " ");
try (FileWriter fw = new FileWriter(DataPath,false))
{
fw.write(values.toString());
fw.close();
}
catch (IOException E)
{
System.out.println("IOException have been Caught");
}
}
DataFile() throws JSONException {
DataPath = "C://Users//Amrutha.JSON";
JSONObject values = new JSONObject();
values.put(" ", " ");
try (FileWriter fw = new FileWriter(DataPath,false))
{
fw.write(values.toString());
fw.close();
}
catch (IOException E)
{
System.out.println("IOException have been Caught");
}
}
//CREATE method when LifeTime value is provided
public void CreateEntrySet(String KeyValue, JSONObject DataValue, int LifeTime) throws Exception
{
try{ if((instrument.getObjectSize((Object)DataValue)/1024)>16)
// Checks whether size of JSONObject is more than 16 KB
throw new ValueSizeExceeded();
else if(KeyValue.length()>32)
throw new KeySizeExceeded();
}
catch (ValueSizeExceeded e) {
System.out.println("Value size exceeds maximum limit");
} catch (KeySizeExceeded e) {
System.out.println("Enter a Valid Key");
}
try (FileReader read = new FileReader(DataPath)) {
JSONTokener tokenValue = new JSONTokener(read);
JSONObject tempValue = new JSONObject(tokenValue);
if (tempValue.has(KeyValue))
//Validating key value pair
throw new DuplicateKey();
JSONArray tArray = new JSONArray();
tArray.put(LifeTime);
// Adding JSONOBject provided by user as first element
tArray.put(DataValue);
// Adding LifeTime value provided by user as second element
LocalTime t = LocalTime.now();
int TimeValue = t.toSecondOfDay();
tArray.put(TimeValue); // Adding time of creation of Key as third element
tempValue.put(KeyValue, tArray);
try (FileWriter fw = new FileWriter(DataPath,false))
{
fw.write(tempValue.toString());
fw.close();
} catch (IOException e) {
System.out.println("IO Exception have been Caught");
}
} catch (DuplicateKey e) {
System.out.println("Duplicate keys have been entered");
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} catch (IOException e) {
System.out.println("IO Exception have been Caught");
}
}
// CREATE method using only Key and Data value pair
public void CreateEntrySet(String KeyValue, JSONObject DataValue) throws Exception
{
try {
if((instrument.getObjectSize((Object)DataValue)/1024)>16)
throw new ValueSizeExceeded();
else if(KeyValue.length()>32)
throw new KeySizeExceeded();
}
catch (ValueSizeExceeded e) {
System.out.println("Value size exceeds maximum limit");
} catch (KeySizeExceeded e) {
System.out.println("Enter a Valid Key");
}
try (FileReader read = new FileReader(DataPath)) {
JSONTokener tokenValue = new JSONTokener(read);
JSONObject tempValue = new JSONObject(tokenValue);
//Validating key value pair
if (tempValue.has(KeyValue))
throw new DuplicateKey();
JSONArray tArray = new JSONArray();
tArray.put(DataValue);
tArray.put(Integer.MAX_VALUE);
LocalTime t = LocalTime.now();
int TimeValue = t.toSecondOfDay();
tArray.put(TimeValue);
tempValue.put(KeyValue, tArray);
//Values are Updated to the file
try (FileWriter fw = new FileWriter(DataPath,false))
{
fw.write(tempValue.toString());
fw.close();
} catch (IOException e) {
System.out.println("IO Exception have been Caught");
}
} catch (DuplicateKey e) {
System.out.println("Duplicate keys have been entered");
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} catch (IOException e) {
System.out.println("IO Exception have been Caught");
}
}
// READ JSON Oject
public JSONObject ReadEntrySet(String KeyValue) throws Exception
{
try (FileReader read = new FileReader(DataPath)) {
JSONTokener tokenValue = new JSONTokener(read);
JSONObject tempValue = new JSONObject(tokenValue);
//Validating key value pair
if (tempValue.has(KeyValue))
{
JSONArray tArray = new JSONArray();
tArray = tempValue.getJSONArray(KeyValue);
LocalTime t = LocalTime.now();
int PresentTime = t.toSecondOfDay();
//Life time of Key is checked
if ((PresentTime - tArray.getInt(2)) < tArray.getInt(1))
return tArray.getJSONObject(0);
else
throw new TimeExceeded();
} else
throw new InvalidKey();
} catch (TimeExceeded e) {
System.out.println("Key Exceeded it's Life Time");
} catch (InvalidKey e) {
System.out.println("Invalid Key");
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} catch (IOException e) {
System.out.println("IO Exception have been Caught");
}
return null;
}
// Delete JSONObject for Key
// Method for deleting a given < Key,JSONOBject > pair
public void DeleteEntrySet(String KeyValue) throws Exception
{
try (FileReader read = new FileReader(DataPath))
{
JSONTokener tokenValue= new JSONTokener(read);
JSONObject tempValue = new JSONObject(tokenValue);
//Validating Key value pair
if (tempValue.has(KeyValue))
{
JSONArray tArray = new JSONArray();
tArray = tempValue.getJSONArray(KeyValue);
LocalTime t = LocalTime.now();
int PresentTime = t.toSecondOfDay();
//Checks the condition of life time of key and removes < Key,Value > pair
if ((PresentTime - tArray.getInt(2)) < tArray.getInt(1))
tempValue.remove(KeyValue);
else
throw new TimeExceeded();
// Edited JSONObject is written back to the file
try (FileWriter fw = new FileWriter(DataPath,false))
{
fw.write(tempValue.toString());
fw.close();
}
}
else
throw new InvalidKey();
}
catch (InvalidKey e) {
System.out.println("Invalid Key");
} catch (IOException e) {
System.out.println("IO Exception has been Caught");
}
catch (TimeExceeded e) {
System.out.println("Key Exceeded it's Life Time");
}
}
}