Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/main/java/com/synopsys/integration/util/PathNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* integration-common
*
* Copyright (C) 2018 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.util;

import java.util.List;

public class PathNode<T> {
private final T key;
private PathNode<T> next;
private PathNode<T> prev;

private PathNode(final T key) {
this.key = key;
this.next = null;
this.prev = null;
}

public static <T> PathNode<T> createPath(final List<T> list) {
PathNode<T> previousNode = null;
PathNode<T> firstNode = null;

for (final T value : list) {
final PathNode currentNode = new PathNode(value);
currentNode.prev = previousNode;
if (firstNode == null) {
firstNode = currentNode;
}
if (previousNode != null) {
previousNode.next = currentNode;
}
previousNode = currentNode;
}
return firstNode;
}

public T getKey() {
return key;
}

public PathNode getNext() {
return next;
}

public PathNode<T> getPrev() {
return prev;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* integration-common
*
* Copyright (C) 2018 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.util.filter;

import java.util.function.Predicate;

public class AndFieldFilterBuilder<T> extends BinaryOperatorFieldFilterBuilder<T> {

public AndFieldFilterBuilder(final FilterBuilder<T> leftFilter, final FilterBuilder<T> rightFilter) {
super(leftFilter, rightFilter, Predicate::and);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* integration-common
*
* Copyright (C) 2018 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.util.filter;

import java.util.function.BiFunction;
import java.util.function.Predicate;

public abstract class BinaryOperatorFieldFilterBuilder<T> implements FilterBuilder<T> {
private final FilterBuilder leftFilterBuilder;
private final FilterBuilder rightFilterBuilder;
private final BiFunction<Predicate<T>, Predicate<T>, Predicate<T>> binaryOperator;

public BinaryOperatorFieldFilterBuilder(final FilterBuilder<T> leftFilterBuilder, final FilterBuilder<T> rightFilterBuilder,
final BiFunction<Predicate<T>, Predicate<T>, Predicate<T>> binaryOperator) {
this.leftFilterBuilder = leftFilterBuilder;
this.rightFilterBuilder = rightFilterBuilder;
this.binaryOperator = binaryOperator;
}

@Override
public final Predicate<T> buildPredicate() {
return binaryOperator.apply(leftFilterBuilder.buildPredicate(), rightFilterBuilder.buildPredicate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* integration-common
*
* Copyright (C) 2018 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.util.filter;

public class DefaultFilterBuilders {
public static final FilterBuilder<?> ALWAYS_TRUE = () -> ignoredPredicateArgument -> true;
public static final FilterBuilder<?> ALWAYS_FALSE = () -> ignoredPredicateArgument -> false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* integration-common
*
* Copyright (C) 2018 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.util.filter;

import java.util.function.Predicate;

public interface FilterBuilder<T> {
Predicate<T> buildPredicate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* integration-common
*
* Copyright (C) 2018 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.util.filter;

import java.util.function.Predicate;

public class OrFieldFilterBuilder<T> extends BinaryOperatorFieldFilterBuilder<T> {

public OrFieldFilterBuilder(final FilterBuilder<T> leftFilter, final FilterBuilder<T> rightFilter) {
super(leftFilter, rightFilter, Predicate::or);
}
}
77 changes: 77 additions & 0 deletions src/main/java/com/synopsys/integration/util/json/JsonAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* integration-common
*
* Copyright (C) 2018 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.util.json;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.synopsys.integration.util.PathNode;

public class JsonAccessor {
protected final Gson gson;

public JsonAccessor(final Gson gson) {
this.gson = gson;
}

public JsonObject createJsonObject(final String json) {
return gson.fromJson(json, JsonObject.class);
}

protected List<JsonElement> getInnerElements(final List<String> fieldNameHierarchy, final JsonObject object) {
final PathNode<String> head = PathNode.createPath(fieldNameHierarchy);
return getInnerElements(object, head);
}

private List<JsonElement> getInnerElements(final JsonElement element, final PathNode<String> pathNode) {
if (element == null) {
return Collections.emptyList();
} else {
if (element.isJsonPrimitive()) {
return Arrays.asList(element);
} else {
if (pathNode != null && element.isJsonObject()) {
final String key = pathNode.getKey();
final JsonObject jsonObject = element.getAsJsonObject();
final JsonElement foundElement = jsonObject.get(key);
return getInnerElements(foundElement, pathNode.getNext());
} else if (element.isJsonArray()) {
final JsonArray foundArray = element.getAsJsonArray();
final List<JsonElement> foundValues = new ArrayList<>(foundArray.size());
for (final JsonElement arrayElement : foundArray) {
foundValues.addAll(getInnerElements(arrayElement, pathNode));
}
return foundValues;
}
return Arrays.asList(element);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* integration-common
*
* Copyright (C) 2018 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.util.json;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.synopsys.integration.util.json.field.HierarchicalField;

public class JsonExtractor extends JsonAccessor {
public JsonExtractor(final Gson gson) {
super(gson);
}

public <T> Optional<T> getFirstValueFromJson(final HierarchicalField field, final String json) {
return getFirstValueFromJsonObject(field, createJsonObject(json));
}

public <T> Optional<T> getFirstValueFromJsonObject(final HierarchicalField field, final JsonObject object) {
final List<T> values = getValuesFromJsonObject(field, object);
return values.stream().findFirst();
}

public <T> List<T> getValuesFromJson(final HierarchicalField field, final String json) {
return getValuesFromJsonObject(field, createJsonObject(json));
}

public <T> List<T> getValuesFromJsonObject(final HierarchicalField field, final JsonObject object) {
final List<JsonElement> foundElements = getInnerElements(field.getPathToInnerField(), object);

final List<T> convertedObjects = new ArrayList<>();
for (final JsonElement element : foundElements) {
final T convertedElement = gson.fromJson(element, field.getType());
convertedObjects.add(convertedElement);
}
return convertedObjects;
}
}
Loading