1
1
package org .json ;
2
2
3
- import java .beans .PropertyChangeListener ;
4
- import java .beans .PropertyChangeSupport ;
5
3
import java .io .Closeable ;
6
4
7
5
/*
@@ -38,7 +36,6 @@ of this software and associated documentation files (the "Software"), to deal
38
36
import java .lang .reflect .Modifier ;
39
37
import java .math .BigDecimal ;
40
38
import java .math .BigInteger ;
41
- import java .util .ArrayList ;
42
39
import java .util .Collection ;
43
40
import java .util .Enumeration ;
44
41
import java .util .HashMap ;
@@ -48,6 +45,7 @@ of this software and associated documentation files (the "Software"), to deal
48
45
import java .util .Objects ;
49
46
import java .util .ResourceBundle ;
50
47
import java .util .Set ;
48
+ import java .util .concurrent .atomic .AtomicBoolean ;
51
49
import java .util .regex .Pattern ;
52
50
53
51
/**
@@ -1793,9 +1791,7 @@ public JSONObject putMap(String key, Map<?, ?> value) throws JSONException {
1793
1791
return this .put (key , new JSONObject (value ));
1794
1792
}
1795
1793
1796
- private PropertyChangeSupport propertyChangeSupportUpdate = new PropertyChangeSupport (this );
1797
- private PropertyChangeSupport propertyChangeSupportNotify = new PropertyChangeSupport (this );
1798
- private static final String propertyChangeGlobalKeyword = "__THIS__" ;
1794
+ private Runnable updateListener = null ;
1799
1795
/**
1800
1796
* Add a PropertyChangeListener to the JSONObject.
1801
1797
* The listener object may be added more than once, and will be called
@@ -1805,32 +1801,8 @@ public JSONObject putMap(String key, Map<?, ?> value) throws JSONException {
1805
1801
*
1806
1802
* @param listener The PropertyChangeListener to be added
1807
1803
*/
1808
- public void onUpdateGlobal (PropertyChangeListener listener ) {
1809
- this .propertyChangeSupportUpdate .addPropertyChangeListener (JSONObject .propertyChangeGlobalKeyword , listener );
1810
- }
1811
-
1812
- /**
1813
- * Add a PropertyChangeListener for a specific property to the JSONObject.
1814
- * The listener will be invoked only when a call on
1815
- * update names that specific property
1816
- * The listener object may be added more than once, and will be called
1817
- * as many times as it is added.
1818
- * If {@code listener} is null, no exception is thrown and no action
1819
- * is taken.
1820
- *
1821
- * @param key The key string to listen on.
1822
- * @param listener The PropertyChangeListener to be added
1823
- */
1824
- public void onUpdate (String key , PropertyChangeListener listener ) {
1825
- if (JSONObject .propertyChangeGlobalKeyword .equals (key )) {
1826
- throw new JSONException ("key \" " + JSONObject .propertyChangeGlobalKeyword + "\" is reserved" );
1827
- } else {
1828
- this .propertyChangeSupportUpdate .addPropertyChangeListener (key , listener );
1829
- }
1830
- }
1831
-
1832
- public void onNotify (String key , PropertyChangeListener listener ) {
1833
- this .propertyChangeSupportNotify .addPropertyChangeListener (key , listener );
1804
+ public void onUpdate (Runnable runnable ) throws JSONException {
1805
+ updateListener = runnable ;
1834
1806
}
1835
1807
1836
1808
/**
@@ -1839,7 +1811,7 @@ public void onNotify(String key, PropertyChangeListener listener) {
1839
1811
*
1840
1812
* @param key
1841
1813
* A key string.
1842
- * @param newValue
1814
+ * @param v2
1843
1815
* An object which is the newValue. It should be of one of these
1844
1816
* types: Boolean, Double, Integer, JSONArray, JSONObject, Long,
1845
1817
* String, or the JSONObject.NULL object.
@@ -1849,60 +1821,38 @@ public void onNotify(String key, PropertyChangeListener listener) {
1849
1821
* @throws JSONException
1850
1822
* If updateListener not initialized.
1851
1823
*/
1852
- public JSONObject update (String key , Object newValue ) throws JSONException {
1853
- final JSONObject oldThis = new JSONObject (this .toString ());
1854
- final Object oldValue = this .opt (key );
1855
-
1856
-
1857
- if (newValue instanceof JSONObject ) {
1858
- this .put (key , newValue );
1859
- final JSONObject __oldThis = new JSONObject (this .toString ());
1860
-
1861
- JSONObject newValueJson = (JSONObject ) newValue ;
1862
-
1863
- newValueJson .onUpdateGlobal (evt -> {
1864
- this .propertyChangeSupportUpdate .firePropertyChange (JSONObject .propertyChangeGlobalKeyword , __oldThis , this );
1865
- this .propertyChangeSupportUpdate .firePropertyChange (key , oldValue , newValue );
1866
-
1867
- this .propertyChangeSupportNotify .firePropertyChange (key , oldValue , newValue );
1868
- });
1869
- } else {
1870
- this .put (key , newValue );
1824
+ public JSONObject update (String key , Object v2 ) throws JSONException {
1825
+ final Object v1 = super .put (key , v2 );
1826
+ if (v2 instanceof JSONObject ) {
1827
+ ((JSONObject ) v2 ).updateListener = this .updateListener ;
1828
+ }
1829
+ if (!(Objects .equals (v2 , v1 ) && Objects .equals (v1 , v2 ))) {
1830
+ this .updateListener .run ();
1871
1831
}
1872
-
1873
-
1874
- this .propertyChangeSupportUpdate .firePropertyChange (JSONObject .propertyChangeGlobalKeyword , oldThis , this );
1875
- this .propertyChangeSupportUpdate .firePropertyChange (key , oldValue , newValue );
1876
-
1877
- this .propertyChangeSupportNotify .firePropertyChange (key , oldValue , newValue );
1878
-
1879
1832
return this ;
1880
1833
}
1881
1834
1882
- public JSONObject notify (String key , Object oldValue , Object newValue ) throws JSONException {
1883
- this .propertyChangeSupportNotify .firePropertyChange (key , oldValue , newValue );
1835
+ public JSONObject synchronize (JSONObject jo ) throws JSONException {
1836
+ jo .forEach ((key , v2 ) -> {
1837
+ this .compute (key , (k , v1 ) -> {
1838
+ if (Objects .equals (v1 , v2 ) && Objects .equals (v2 , v1 )) {
1839
+ return v1 ;
1840
+ } else {
1841
+ return JSONObject .NULL .equals (v2 ) ? JSONObject .NULL : v2 ;
1842
+ }
1843
+ });
1844
+ });
1884
1845
1885
- return this ;
1886
- }
1846
+ final String [] names = JSONObject .getNames (this );
1847
+ if (names != null ) {
1848
+ for (String key : names ) {
1849
+ if (!jo .has (key )) {
1850
+ this .remove (key );
1851
+ }
1852
+ }
1853
+ }
1887
1854
1888
- /**
1889
- * Put a key/value pair in the JSONObject and
1890
- * reports a bound property update to listeners.
1891
- *
1892
- * @param key
1893
- * A key string.
1894
- * @param newValue
1895
- * An object which is the newValue. It should be of one of these
1896
- * types: Boolean, Double, Integer, JSONArray, JSONObject, Long,
1897
- * String, or the JSONObject.NULL object.
1898
- * @return this.
1899
- * @throws JSONException
1900
- * If the newValue is non-finite number.
1901
- * @throws JSONException
1902
- * If updateListener not initialized.
1903
- */
1904
- public JSONObject update (JSONObject jo ) throws JSONException {
1905
- return this .updateOrRemove (jo , false , true );
1855
+ return this ;
1906
1856
}
1907
1857
1908
1858
/**
@@ -1921,63 +1871,32 @@ public JSONObject update(JSONObject jo) throws JSONException {
1921
1871
* @throws JSONException
1922
1872
* If updateListener not initialized.
1923
1873
*/
1924
- public JSONObject updateOrRemove (JSONObject jo ) throws JSONException {
1925
- return this .updateOrRemove (jo , true , true );
1926
- }
1927
-
1928
- public JSONObject mix (JSONObject jo ) throws JSONException {
1929
- return this .updateOrRemove (jo , false , false );
1930
- }
1931
-
1932
- public JSONObject mixOrRemove (JSONObject jo ) throws JSONException {
1933
- return this .updateOrRemove (jo , true , false );
1934
- }
1935
-
1936
- private JSONObject updateOrRemove (JSONObject jo , boolean remove , boolean triggerUpdate ) throws JSONException {
1937
- final JSONObject oldThis = new JSONObject (this .toString ());
1938
-
1939
- final HashMap <String , Object > oldValues = new HashMap <String , Object >();
1940
- final HashMap <String , Object > newValues = new HashMap <String , Object >();
1941
- final ArrayList <String > delValues = new ArrayList <String >();
1874
+ public JSONObject updateSynchronize (JSONObject jo ) throws JSONException {
1875
+ final AtomicBoolean changed = new AtomicBoolean (false );
1942
1876
1943
1877
jo .forEach ((key , v2 ) -> {
1944
1878
this .compute (key , (k , v1 ) -> {
1945
1879
if (Objects .equals (v1 , v2 ) && Objects .equals (v2 , v1 )) {
1946
1880
return v1 ;
1947
1881
} else {
1948
- oldValues .put (key , JSONObject .NULL .equals (v1 ) ? null : v1 );
1949
- newValues .put (key , JSONObject .NULL .equals (v2 ) ? null : v2 );
1882
+ changed .set (true );
1950
1883
return JSONObject .NULL .equals (v2 ) ? JSONObject .NULL : v2 ;
1951
1884
}
1952
1885
});
1953
1886
});
1954
1887
1955
- if (remove ) {
1956
- final String [] names = JSONObject .getNames (this );
1957
- if (names != null ) {
1958
- for (String key : names ) {
1959
- if (!jo .has (key )) {
1960
- oldValues .put (key , this .remove (key ));
1961
- delValues .add (key );
1962
- }
1888
+ final String [] names = JSONObject .getNames (this );
1889
+ if (names != null ) {
1890
+ for (String key : names ) {
1891
+ if (!jo .has (key )) {
1892
+ changed .set (true );
1893
+ this .remove (key );
1963
1894
}
1964
1895
}
1965
1896
}
1966
1897
1967
- if (oldValues .size () > 0 ) {
1968
- if (triggerUpdate ) {
1969
- this .propertyChangeSupportUpdate .firePropertyChange (JSONObject .propertyChangeGlobalKeyword , oldThis , this );
1970
- }
1971
-
1972
- oldValues .forEach ((key , oldValue ) -> {
1973
- final Object v2 = delValues .contains (key ) ? null : newValues .get (key );
1974
- final Object v1 = oldValue == null && v2 == null ? JSONObject .NULL : oldValue ;
1975
-
1976
- if (triggerUpdate ) {
1977
- this .propertyChangeSupportUpdate .firePropertyChange (key , v1 , v2 );
1978
- }
1979
- this .propertyChangeSupportNotify .firePropertyChange (key , v1 , v2 );
1980
- });
1898
+ if (changed .get ()) {
1899
+ this .updateListener .run ();
1981
1900
}
1982
1901
1983
1902
return this ;
0 commit comments