Skip to content

Commit c0ca196

Browse files
committed
- Add toJson() method to the Field class.
- Updated JSON output in the toJson() method in the Record class. - Added new setInterval() and setTimeout() methods to the Timer class. - Minor documentation updates in the Directory class. git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1586 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 289a19e commit c0ca196

File tree

4 files changed

+209
-36
lines changed

4 files changed

+209
-36
lines changed

src/javaxt/io/Directory.java

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,9 @@ public int compareTo(Object obj){
17331733
//**************************************************************************
17341734
//** equals
17351735
//**************************************************************************
1736-
1736+
/** Returns true if the given Object is a Directory (or java.io.File) that
1737+
* refers to the current directory.
1738+
*/
17371739
public boolean equals(Object obj){
17381740
if (obj instanceof Directory){
17391741
return getFile().equals(((Directory) obj).toFile());
@@ -1764,42 +1766,55 @@ public Directory clone(){
17641766
//** getEvents
17651767
//**************************************************************************
17661768
/** Used to start monitoring changes made to the directory. Changes include
1767-
* creating, modifying or deleting files/folders found in this directory.
1768-
* Returns a list of Directory.Event(s). Clients can wait for new events
1769-
* using the wait() method. Recommend removing events from the list whenever
1770-
* !events.isEmpty().
1771-
*
1772-
* Example:<pre>
1769+
* creating, modifying, or deleting files/folders found in this directory.
1770+
* Returns a list of Directory.Event(s). Caller can wait for new events
1771+
* using the wait() method like this:
1772+
<pre>
17731773
java.util.List events = directory.getEvents();
17741774
while (true){
1775-
1776-
Object obj;
1775+
Directory.Event event;
17771776
synchronized (events) {
1778-
while (events.isEmpty()) {
1779-
try {
1780-
events.wait();
1781-
}
1782-
catch (InterruptedException e) {
1783-
}
1777+
synchronized (events) {
1778+
while (events.isEmpty()) {
1779+
try {
1780+
events.wait();
1781+
}
1782+
catch (InterruptedException e) {}
1783+
}
1784+
event = (Directory.Event) events.remove(0);
17841785
}
1785-
1786-
obj = events.remove(0);
17871786
}
1788-
if (obj!=null){
17891787
1790-
javaxt.io.Directory.Event event = (javaxt.io.Directory.Event) obj;
1788+
if (event!=null){
17911789
System.out.println(event.toString());
1792-
1793-
//Compare files before/after the event
17941790
if (event.getEventID()==event.RENAME){
17951791
System.out.println(
1796-
event.getOriginalFile().getName() + " vs " +
1797-
event.getFile().getName()
1792+
event.getOriginalFile() + " vs " +
1793+
event.getFile()
17981794
);
17991795
}
18001796
}
1797+
}
1798+
</pre>
1799+
* Note that in this example, we are removing events from the list in the
1800+
* order that they occur. It is critical to remove events from the list to
1801+
* as quickly as possible to avoid potential memory issues. Use the stop()
1802+
* method to stop monitoring events. Obviously you will need to start the
1803+
* monitor in a separate thread in order to call the stop() method. Example:
1804+
<pre>
1805+
1806+
//Start directory monitor
1807+
new Thread(() -> {
1808+
java.util.List events = directory.getEvents();
1809+
while (true){
1810+
...
1811+
}
1812+
}).start();
1813+
18011814
1802-
}</pre>
1815+
//Stop directory monitor
1816+
directory.stop();
1817+
</pre>
18031818
*/
18041819
public List getEvents() throws Exception {
18051820

@@ -1842,8 +1857,9 @@ protected void finalize() throws Throwable {
18421857
//**************************************************************************
18431858
//** Event Class
18441859
//**************************************************************************
1845-
/** Used to encapsulate a single event on the file system.
1846-
*/
1860+
/** Used to encapsulate an event on the file system (e.g. create, update,
1861+
* rename, or delete).
1862+
*/
18471863
public static class Event {
18481864

18491865
private String file;
@@ -1931,7 +1947,8 @@ protected void setAction(String action){
19311947
//************************************************************************
19321948
//** getFile
19331949
//************************************************************************
1934-
/** Returns the file or directory that was created, modified, or deleted.
1950+
/** Returns the path of the file or directory that was created, modified,
1951+
* or deleted.
19351952
*/
19361953
public String getFile(){
19371954
return file;
@@ -1941,6 +1958,9 @@ public String getFile(){
19411958
//************************************************************************
19421959
//** getOriginalFile
19431960
//************************************************************************
1961+
/** If a file or directory was moved or renamed, returns the path to the
1962+
* original file or directory.
1963+
*/
19441964
public String getOriginalFile(){
19451965
return orgFile;
19461966
}
@@ -1964,17 +1984,17 @@ public final int getEventID(){
19641984
//************************************************************************
19651985
//** getDate
19661986
//************************************************************************
1967-
/** Returns the date/time stamp when the event occured. */
1968-
1987+
/** Returns the date/time stamp when the event occurred.
1988+
*/
19691989
public java.util.Date getDate(){
19701990
return date;
19711991
}
19721992

19731993
//************************************************************************
19741994
//** toString
19751995
//************************************************************************
1976-
/** Returns a string representation of this event. */
1977-
1996+
/** Returns a string representation of this event.
1997+
*/
19781998
public String toString(){
19791999
if (action.equalsIgnoreCase("rename"))
19802000
return "[" + date.toString() + "] " + action + " " + orgFile + " To " + file;

src/javaxt/sql/Field.java

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package javaxt.sql;
2+
import javaxt.json.JSONArray;
3+
import javaxt.json.JSONObject;
24

35
//******************************************************************************
46
//** Field Class
@@ -32,7 +34,7 @@ protected Field(int i, java.sql.ResultSetMetaData rsmd){
3234

3335

3436
//Special case. Discovered that the column name was returning a
35-
//table prefix when performing a union quiries with SQLite
37+
//table prefix when performing a union queries with SQLite
3638
if (name!=null && name.contains(".")){
3739
String[] arr = name.split("\\.");
3840
if (arr.length==3){
@@ -60,6 +62,9 @@ private Field(){}
6062
//**************************************************************************
6163
//** clone
6264
//**************************************************************************
65+
/** Used to create a shallow copy of the current field. Does not include the
66+
* field value.
67+
*/
6368
public Field clone(){
6469
Field field = new Field();
6570
field.name = name;
@@ -188,14 +193,85 @@ protected void requiresUpdate(boolean b){
188193
requiresUpdate = b;
189194
}
190195

196+
191197
//**************************************************************************
192198
//** toString
193199
//**************************************************************************
200+
/** Returns the field name.
201+
*/
194202
public String toString(){
195203
return name;
196204
}
197205

198206

207+
//**************************************************************************
208+
//** toJson
209+
//**************************************************************************
210+
/** Returns a JSON representation of the field.
211+
*/
212+
public JSONObject toJson(){
213+
JSONObject json = new JSONObject();
214+
215+
216+
Value val = getValue();
217+
if (!val.isNull()){
218+
Object obj = val.toObject();
219+
Class cls = obj.getClass();
220+
String className = cls.getSimpleName();
221+
Package pkg = cls.getPackage();
222+
String packageName = pkg==null ? "" : pkg.getName();
223+
224+
225+
//Special case for json objects
226+
if ((packageName.equals("java.lang") && className.equals("String")) ||
227+
!packageName.startsWith("java"))
228+
{
229+
String s = obj.toString().trim();
230+
if (s.startsWith("{") && s.endsWith("}")){
231+
try{
232+
val = new Value(new JSONObject(s));
233+
}
234+
catch(Exception e){}
235+
}
236+
else if (s.startsWith("[") && s.endsWith("]")){
237+
try{
238+
val = new Value(new JSONArray(s));
239+
}
240+
catch(Exception e){}
241+
}
242+
}
243+
244+
245+
//Special case for arrays
246+
if (obj instanceof java.sql.Array){
247+
try{
248+
JSONArray arr = new JSONArray();
249+
for (Object o : (Object[]) ((java.sql.Array) obj).getArray()){
250+
arr.add(o);
251+
}
252+
val = new Value(arr);
253+
}
254+
catch(Exception e){}
255+
}
256+
257+
258+
//Special case for H2's TimestampWithTimeZone
259+
if (packageName.equals("org.h2.api")){
260+
if (className.equals("TimestampWithTimeZone")){
261+
val = new Value(val.toDate());
262+
}
263+
}
264+
}
265+
266+
json.set("name", name);
267+
json.set("value", val);
268+
json.set("type", type);
269+
json.set("table", tableName);
270+
json.set("schema", schema);
271+
return json;
272+
}
273+
274+
199275
//**************************************************************************
200276
//** clear
201277
//**************************************************************************

src/javaxt/sql/Record.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import javaxt.json.JSONObject;
33
import java.util.HashMap;
44
import java.util.ArrayList;
5+
import javaxt.json.JSONArray;
56

67
//******************************************************************************
78
//** Record Class
@@ -216,7 +217,8 @@ public boolean isDirty(){
216217
public JSONObject toJson(){
217218
JSONObject json = new JSONObject();
218219
for (Field field : fields){
219-
json.set(field.getName(), field.getValue());
220+
JSONObject f = field.toJson();
221+
json.set(field.getName(), f.get("value"));
220222
}
221223
return json;
222224
}

src/javaxt/utils/Timer.java

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package javaxt.utils;
2+
import java.util.TimerTask;
23
import java.util.concurrent.ScheduledFuture;
34
import java.util.concurrent.ScheduledThreadPoolExecutor;
45
import java.util.concurrent.TimeUnit;
@@ -57,7 +58,7 @@ public void schedule(Runnable task, java.util.Date time) {
5758
//**************************************************************************
5859
//** scheduleAtFixedRate
5960
//**************************************************************************
60-
/** Schedules the specified task for repeated fixed-rate execution,
61+
/** Schedules the specified task for repeated fixed-rate execution,
6162
* beginning at the specified time. Subsequent executions take place at
6263
* regular intervals, separated by the specified period.
6364
*
@@ -89,14 +90,88 @@ public void scheduleAtFixedRate(Runnable task, long delay, long period){
8990
}
9091

9192

93+
//**************************************************************************
94+
//** cancel
95+
//**************************************************************************
96+
/** Used to shutdown the current timer task
97+
*/
9298
public void cancel(){
9399
scheduler.shutdown();
94100
}
95101

102+
103+
//**************************************************************************
104+
//** initialized
105+
//**************************************************************************
106+
/** Returns true if the time task has been initialized
107+
*/
96108
public boolean initialized(){
97109
return scheduler.task!=null;
98110
}
99111

112+
113+
//**************************************************************************
114+
//** setInterval
115+
//**************************************************************************
116+
/** Used to repeatedly call a given function/callback, with a fixed time
117+
* delay between each call. Example:
118+
<pre>
119+
setInterval(()->{
120+
System.out.println(new java.util.Date());
121+
}, 1000);
122+
</pre>
123+
* @param interval Time in milliseconds between successive calls to the
124+
* callback.
125+
*/
126+
public static Timer setInterval(Callback callback, long interval){
127+
Timer timer = new Timer();
128+
new Thread(() -> {
129+
timer.scheduleAtFixedRate(new TimerTask(){
130+
public void run(){
131+
callback.call();
132+
}
133+
}, 0, interval);
134+
}).start();
135+
return timer;
136+
}
137+
138+
139+
//**************************************************************************
140+
//** setTimeout
141+
//**************************************************************************
142+
/** Used to call a given function/callback after a delay. Example:
143+
<pre>
144+
long startTime = System.currentTimeMillis();
145+
setTimeout(()->{
146+
System.out.println(System.currentTimeMillis()-startTime);
147+
}, 1000);
148+
</pre>
149+
* @param delay Delay in milliseconds before the callback is called.
150+
*/
151+
public static Timer setTimeout(Callback callback, long delay){
152+
Timer timer = new Timer();
153+
new Thread(() -> {
154+
timer.scheduleAtFixedRate(new TimerTask(){
155+
public void run(){
156+
callback.call();
157+
}
158+
}, delay, 0);
159+
}).start();
160+
return timer;
161+
}
162+
163+
164+
//**************************************************************************
165+
//** Callback Interface
166+
//**************************************************************************
167+
/** Implementations of this class are used by the static setInterval() and
168+
* setTimeout() methods.
169+
*/
170+
public static interface Callback {
171+
public void call();
172+
}
173+
174+
100175
//**************************************************************************
101176
//** Scheduler Class
102177
//**************************************************************************
@@ -139,7 +214,7 @@ public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay,
139214
return init();
140215
}
141216

142-
217+
143218
@Override
144219
public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
145220

@@ -151,7 +226,7 @@ public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDela
151226

152227
return init();
153228
}
154-
229+
155230

156231
private ScheduledFuture init(){
157232
if (task.equals("schedule")){

0 commit comments

Comments
 (0)