@@ -3,7 +3,6 @@ package wu.seal.jsontokotlin.utils
33import com.google.gson.JsonArray
44import java.awt.Component
55import java.awt.Container
6- import java.util.regex.Matcher
76import java.util.regex.Pattern
87import javax.swing.Box
98import javax.swing.BoxLayout
@@ -24,7 +23,7 @@ fun Container.addComponentIntoVerticalBoxAlignmentLeft(component: Component) {
2423
2524}
2625
27- fun Container.addComponentIntoVerticalBoxAlignmentLeft (component : Component , leftMargin : Int ) {
26+ fun Container.addComponentIntoVerticalBoxAlignmentLeft (component : Component , leftMargin : Int ) {
2827 if (layout is BoxLayout ) {
2928
3029 val hBox = Box .createHorizontalBox()
@@ -40,10 +39,10 @@ fun Container.addComponentIntoVerticalBoxAlignmentLeft(component: Component, lef
4039/* *
4140 * How many substring in the parent string
4241 */
43- fun String.numberOf (subString : String ):Int {
42+ fun String.numberOf (subString : String ): Int {
4443 var count = 0
4544 val pattern = Pattern .compile(subString)
46- val matcher= pattern.matcher(this )
45+ val matcher = pattern.matcher(this )
4746 while (matcher.find()) {
4847 count++
4948 }
@@ -57,6 +56,25 @@ private fun JsonArray.onlyHasOneElement(): Boolean {
5756 return size() == 1
5857}
5958
59+ /* *
60+ * array only has one object element
61+ */
62+ private fun JsonArray.onlyHasOneObjectElement (): Boolean {
63+ return size() == 1 && get(0 ).isJsonObject
64+ }
65+
66+ /* *
67+ * array only has object element
68+ */
69+ private fun JsonArray.allObjectElement (): Boolean {
70+ forEach {
71+ if (it.isJsonObject.not ()) {
72+ return false
73+ }
74+ }
75+ return true
76+ }
77+
6078/* *
6179 * if Multidimensional Arrays only has one element
6280 */
@@ -74,4 +92,48 @@ fun JsonArray.onlyHasOneElementRecursive(): Boolean {
7492 }
7593
7694 return get(0 ).asJsonArray.onlyHasOneElementRecursive()
95+ }
96+
97+
98+ /* *
99+ * if Multidimensional Arrays only has one element
100+ */
101+ fun JsonArray.onlyHasOneObjectElementRecursive (): Boolean {
102+
103+ if (size() == 0 ) {
104+ return false
105+ }
106+ if (onlyHasOneElement().not ()) {
107+ return false
108+ }
109+
110+ if (get(0 ).isJsonPrimitive || get(0 ).isJsonNull) {
111+ return false
112+ }
113+
114+ if (get(0 ).isJsonObject) {
115+ return true
116+ }
117+ return get(0 ).asJsonArray.onlyHasOneObjectElementRecursive()
118+ }
119+
120+
121+ /* *
122+ * if Multidimensional Arrays only has one dimension contains element and the elements are all object element
123+ */
124+ fun JsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive (): Boolean {
125+
126+ if (size() == 0 ) {
127+ return false
128+ }
129+
130+ if (get(0 ).isJsonPrimitive || get(0 ).isJsonNull) {
131+ return false
132+ }
133+
134+ if (allObjectElement()) {
135+ return true
136+ }
137+
138+ return get(0 ).asJsonArray.onlyOneSubArrayContainsElementAndAllObjectRecursive()
77139}
0 commit comments