Skip to content

Commit

Permalink
Probe JSON generator - simplify the use a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba authored and jharting committed Feb 20, 2015
1 parent 30b7676 commit 990cfe1
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 83 deletions.
88 changes: 62 additions & 26 deletions probe/core/src/main/java/org/jboss/weld/probe/Json.java
Expand Up @@ -21,17 +21,18 @@
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;

import org.jboss.weld.util.Preconditions;

/**
* A simple JSON generator.
* Simple JSON generator. A third-party library is not used intentionally - we don't need any other dependencies.
*
* @author Martin Kouba
*/
class Json {
final class Json {

private static final String NAME = "name";
private static final String OBJECT_START = "{";
Expand All @@ -47,12 +48,39 @@ class Json {
private Json() {
}

public static JsonArrayBuilder newArrayBuilder() {
return new JsonArrayBuilder();
/**
* @return the new JSON array builder, empty builders are not ignored
*/
static JsonArrayBuilder arrayBuilder() {
return new JsonArrayBuilder(false);
}

public static JsonObjectBuilder newObjectBuilder() {
return new JsonObjectBuilder();
/**
*
* @param ignoreEmptyBuilders
* @return the new JSON array builder
* @see JsonBuilder#ignoreEmptyBuilders
*/
static JsonArrayBuilder arrayBuilder(boolean ignoreEmptyBuilders) {
return new JsonArrayBuilder(ignoreEmptyBuilders);
}

/**
*
* @return the new JSON object builder, empty builders are not ignored
*/
static JsonObjectBuilder objectBuilder() {
return new JsonObjectBuilder(false);
}

/**
*
* @param ignoreEmptyBuilders
* @return the new JSON object builder
* @see JsonBuilder#ignoreEmptyBuilders
*/
static JsonObjectBuilder objectBuilder(boolean ignoreEmptyBuilders) {
return new JsonObjectBuilder(ignoreEmptyBuilders);
}

/**
Expand All @@ -67,25 +95,23 @@ abstract static class JsonBuilder<T> {

/**
*
* @return <code>true</code> if there are no elements/properties, <code>false</code> otherwise
* @param ignoreEmptyBuilders If set to true all empty builders added to this builder will be ignored during {@link #build()}
*/
abstract boolean isEmpty();
JsonBuilder(boolean ignoreEmptyBuilders) {
this.ignoreEmptyBuilders = ignoreEmptyBuilders;
}

/**
*
* @return a string representation
* @return <code>true</code> if there are no elements/properties, <code>false</code> otherwise
*/
abstract String build();
abstract boolean isEmpty();

/**
*
* @param value If set to true all empty builders will be ignored from now on
* @return self
* @return a string representation
*/
T setIgnoreEmptyBuilders(boolean value) {
this.ignoreEmptyBuilders = value;
return self();
}
abstract String build();

/**
*
Expand Down Expand Up @@ -127,7 +153,8 @@ static class JsonArrayBuilder extends JsonBuilder<JsonArrayBuilder> {

private final List<Object> values;

private JsonArrayBuilder() {
private JsonArrayBuilder(boolean ignoreEmptyBuilders) {
super(ignoreEmptyBuilders);
this.values = new ArrayList<Object>();
}

Expand Down Expand Up @@ -162,7 +189,7 @@ JsonArrayBuilder add(Long value) {
}

private void addInternal(Object value) {
if (!isIgnored(value)) {
if (value != null) {
values.add(value);
}
}
Expand All @@ -174,12 +201,16 @@ boolean isEmpty() {
String build() {
StringBuilder builder = new StringBuilder();
builder.append(ARRAY_START);
for (Iterator<Object> iterator = values.iterator(); iterator.hasNext();) {
int idx = 0;
for (ListIterator<Object> iterator = values.listIterator(); iterator.hasNext();) {
Object value = iterator.next();
appendValue(builder, value);
if (iterator.hasNext()) {
if(isIgnored(value)) {
continue;
}
if (++idx > 1) {
builder.append(ENTRY_SEPARATOR);
}
appendValue(builder, value);
}
builder.append(ARRAY_END);
return builder.toString();
Expand All @@ -201,7 +232,8 @@ static class JsonObjectBuilder extends JsonBuilder<JsonObjectBuilder> {

private final Map<String, Object> properties;

private JsonObjectBuilder() {
private JsonObjectBuilder(boolean ignoreEmptyBuilders) {
super(ignoreEmptyBuilders);
this.properties = new LinkedHashMap<String, Object>();
}

Expand Down Expand Up @@ -237,7 +269,7 @@ JsonObjectBuilder add(String name, Long value) {

private void addInternal(String name, Object value) {
Preconditions.checkArgumentNotNull(name, NAME);
if (!isIgnored(value)) {
if (value != null) {
properties.put(name, value);
}
}
Expand All @@ -252,14 +284,18 @@ boolean isEmpty() {
String build() {
StringBuilder builder = new StringBuilder();
builder.append(OBJECT_START);
int idx = 0;
for (Iterator<Entry<String, Object>> iterator = properties.entrySet().iterator(); iterator.hasNext();) {
Entry<String, Object> entry = iterator.next();
if(isIgnored(entry.getValue())) {
continue;
}
if (++idx > 1) {
builder.append(ENTRY_SEPARATOR);
}
appendStringValue(builder, entry.getKey());
builder.append(NAME_VAL_SEPARATOR);
appendValue(builder, entry.getValue());
if (iterator.hasNext()) {
builder.append(ENTRY_SEPARATOR);
}
}
builder.append(OBJECT_END);
return builder.toString();
Expand Down

0 comments on commit 990cfe1

Please sign in to comment.