Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: get rid of Guava #669

Merged
merged 3 commits into from
Jan 9, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.util.Map;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.HttpClient;
import org.eclipse.lyo.core.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -58,7 +58,7 @@ public OslcOAuthClient(

oauthRealmName = "Jazz";
// Change if a different name was detected
if (!StringUtils.isEmpty(realm)) {
if (!StringUtils.isNullOrEmpty(realm)) {
oauthRealmName = realm;
}

Expand Down
4 changes: 0 additions & 4 deletions core/lyo-core-settings/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@
<artifactId>jersey-common</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License 1.0
* which is available at http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.lyo.core.util;

import java.text.Normalizer;
import java.util.regex.Pattern;

/**
* @since 7.0.0
*/
public class StringUtils {
/**
* Pattern to match control characters in the Unicode Cc category that are not CR, LF, or TAB
*/
private static final Pattern CONTROL_CHAR_PATTERN = Pattern.compile("^\\p{Cc}&&[^\\r\\n\\t]+$");

/**
* Trim and strip control chars (in the Unicode Cc category that are not CR, LF, or TAB)
*/
public static String cleanWithoutNormalization(String str) {
if (str == null) return null;

return CONTROL_CHAR_PATTERN.matcher(str).replaceAll("").trim();
}

/**
* Trim and strip control chars (in the Unicode Cc category that are not CR, LF, or TAB);
* returns an empty string if a null is encountered
*/
public static String cleanWithoutNormalizationNonNull(String str) {
if (str == null) return "";

return CONTROL_CHAR_PATTERN.matcher(str).replaceAll("").trim();
}

/**
* Trim, strip control chars (in the Unicode Cc category that are not CR, LF, or TAB), and
* normalize the string to NFC as per W3C recommendations
*/
public static String clean(String str) {
if (str == null) return null;

return Normalizer.normalize(CONTROL_CHAR_PATTERN.matcher(str).replaceAll("").trim(),
Normalizer.Form.NFC);
}

/**
* Trim, strip control chars (in the Unicode Cc category that are not CR, LF, or TAB), and
* normalize the string to NFC as per W3C recommendations;
* returns an empty string if a null is encountered
*/
public static String cleanNonNull(String str) {
if (str == null) return "";

return Normalizer.normalize(CONTROL_CHAR_PATTERN.matcher(str).replaceAll("").trim(),
Normalizer.Form.NFC);
}

public static boolean isNullOrWhitespace(String str) {
return str == null || str.isBlank();
}

public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import com.google.common.base.Strings;
import org.apache.jena.datatypes.DatatypeFormatException;
import org.apache.jena.datatypes.RDFDatatype;
import org.apache.jena.datatypes.TypeMapper;
import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.datatypes.xsd.XSDDateTime;
import org.apache.jena.datatypes.xsd.impl.XMLLiteralType;
import org.apache.jena.rdf.model.Property;
import org.eclipse.lyo.core.util.StringUtils;
import org.eclipse.lyo.oslc4j.core.model.ResourceShape;
import org.eclipse.lyo.oslc4j.core.model.XMLLiteral;
import org.slf4j.Logger;
Expand Down Expand Up @@ -550,7 +550,7 @@ private static Boolean parseBooleanPropertyOrDefault(final String key,
final boolean defaultValue) {
Boolean value;
final String property = System.getProperty(key);
if (Strings.isNullOrEmpty(property)) {
if (StringUtils.isNullOrEmpty(property)) {
value = defaultValue;
} else {
try {
Expand Down
5 changes: 0 additions & 5 deletions core/oslc4j-jena-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@
<version>${v.jersey}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.List;
import javax.xml.datatype.DatatypeConfigurationException;

import com.google.common.collect.ImmutableList;
import org.apache.jena.datatypes.DatatypeFormatException;
import org.apache.jena.rdf.model.Model;
import org.eclipse.lyo.oslc4j.core.exception.LyoModelException;
Expand Down Expand Up @@ -53,7 +53,7 @@ public void testSeqMarshalling()
final Model expectedModel = RDFHelper.loadResourceModel("container-element.ttl");
final Container container = new Container();
container.setAbout(URI.create("urn:containerA"));
final ImmutableList<Element> children = ImmutableList.of(element("A"), element("B"));
final List<Element> children = List.of(element("A"), element("B"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List.of produces an immutable list out of the box.

container.setChildrenL(children);
container.setChildrenB(children);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.collect.ImmutableList;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.oslc4j.core.model.ServiceProvider;
import org.eclipse.lyo.oslc4j.provider.jena.OslcJsonLdArrayProvider;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void testWrite() throws Exception {
.getAnnotations(), OslcMediaType.APPLICATION_JSON_LD_TYPE, new
MultivaluedHashMap<>(), outputStream);

final String jsonLD = outputStream.toString("UTF-8");
final String jsonLD = outputStream.toString(StandardCharsets.UTF_8);

assertTrue("Provider was not read", jsonLD.contains("Hello world"));

Expand All @@ -94,7 +95,7 @@ public void testWriteArray() throws Exception {
new MultivaluedHashMap<>(),
outputStream);

final String jsonLD = outputStream.toString("UTF-8");
final String jsonLD = outputStream.toString(StandardCharsets.UTF_8);

assertTrue("Provider was not read", jsonLD.contains("Hello world"));
}
Expand All @@ -107,7 +108,7 @@ public void testWriteCollection() throws Exception {

ServiceProvider sp = new ServiceProvider();
sp.setDescription("Hello world");
final Collection<ServiceProvider> objects = ImmutableList.of(sp);
final Collection<ServiceProvider> objects = List.of(sp);
provider.writeTo(
new ArrayList<>(objects),
objects.getClass(),
Expand All @@ -117,7 +118,7 @@ public void testWriteCollection() throws Exception {
new MultivaluedHashMap<>(),
outputStream);

final String jsonLD = outputStream.toString("UTF-8");
final String jsonLD = outputStream.toString(StandardCharsets.UTF_8);

assertTrue("Provider was not read", jsonLD.contains("Hello world"));
}
Expand Down
4 changes: 0 additions & 4 deletions core/shacl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
<artifactId>oslc4j-jena-provider</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 1 addition & 4 deletions core/shacl/src/main/java/org/eclipse/lyo/shacl/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.eclipse.lyo.shacl;

import com.google.common.collect.ImmutableList;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
Expand All @@ -26,7 +25,6 @@

/**
* @author Yash Khatri
* @version $version-stub$
* @since 2.3.0
*/
@OslcNamespace(ShaclConstants.SHACL_CORE_NAMESPACE)
Expand Down Expand Up @@ -133,8 +131,7 @@ public void setTargetObjectsOf(final URI targetObjectsOf) {
@OslcTitle("Properties")
@OslcValueType(ValueType.LocalResource)
public List<Property> getShaclProperties() {
return ImmutableList.copyOf(
properties.values().toArray(new Property[properties.size()]));
return List.of(properties.values().toArray(new Property[0]));
}

public void setShaclProperties(final List<Property> properties) {
Expand Down
8 changes: 0 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
<v.jackson>2.18.2</v.jackson>
<v.httpclient>4.5.14</v.httpclient>
<v.slf4j>2.0.16</v.slf4j>

<v.guava>33.3.1-jre</v.guava>
</properties>


Expand Down Expand Up @@ -393,11 +391,6 @@
<artifactId>commons-codec</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${v.guava}</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
Expand Down Expand Up @@ -476,7 +469,6 @@
<link>https://jakarta.ee/specifications/platform/9/apidocs/</link>
<link>https://jena.apache.org/documentation/javadoc/jena/</link>
<link>https://jena.apache.org/documentation/javadoc/arq/</link>
<link>https://guava.dev/releases/${v.guava}/api/docs/</link>
</links>
</configuration>
<executions>
Expand Down
4 changes: 0 additions & 4 deletions server/oslc-ui-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.lyo.core.util.StringUtils;
import org.eclipse.lyo.oslc4j.core.annotation.OslcName;
import org.eclipse.lyo.oslc4j.core.annotation.OslcOccurs;
import org.eclipse.lyo.oslc4j.core.annotation.OslcPropertyDefinition;
Expand Down Expand Up @@ -143,7 +143,7 @@ private static org.eclipse.lyo.server.ui.model.Link constructLink(Link link) {
if (null == link) {
return null;
}
if (StringUtils.isBlank(link.getLabel())) {
if (StringUtils.isNullOrWhitespace(link.getLabel())) {
return constructLink(link.getValue().toString(), link.getValue().toString());
} else {
return constructLink(link.getValue().toString(), link.getLabel());
Expand Down
4 changes: 0 additions & 4 deletions store/store-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<!-- Test -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

import org.apache.commons.lang3.StringUtils;
import org.apache.jena.arq.querybuilder.DescribeBuilder;
import org.apache.jena.arq.querybuilder.ExprFactory;
import org.apache.jena.arq.querybuilder.Order;
Expand Down Expand Up @@ -67,6 +66,7 @@
import org.eclipse.lyo.core.query.UriRefValue;
import org.eclipse.lyo.core.query.Value;
import org.eclipse.lyo.core.query.WhereClause;
import org.eclipse.lyo.core.util.StringUtils;
import org.eclipse.lyo.oslc4j.core.OSLC4JUtils;
import org.eclipse.lyo.oslc4j.core.annotation.OslcName;
import org.eclipse.lyo.oslc4j.core.annotation.OslcNamespace;
Expand Down Expand Up @@ -295,8 +295,8 @@ public <T extends IResource> List<T> getResources(final URI namedGraph, final Cl
String _prefixes = prefixes;
String _where = where;

_prefixes = (StringUtils.isEmpty(_prefixes) ? "" : _prefixes + ",") + oslcQueryPrefixes(clazz);
_where = (StringUtils.isEmpty(_where) ? "" : _where + " and ") + oslcQueryWhere(clazz);
_prefixes = (StringUtils.isNullOrEmpty(_prefixes) ? "" : _prefixes + ",") + oslcQueryPrefixes(clazz);
_where = (StringUtils.isNullOrEmpty(_where) ? "" : _where + " and ") + oslcQueryWhere(clazz);
Model model = getResources(namedGraph, _prefixes, _where, searchTerms, limit, offset, additionalDistinctVars,
additionalQueryFilter);
return getResourcesFromModel(model, clazz);
Expand Down Expand Up @@ -595,7 +595,7 @@ private SelectBuilder constructSparqlWhere(final String prefixes, final String w
//Setup prefixes
Map<String, String> prefixesMap = new HashMap<>();
try {
if (!StringUtils.isEmpty(prefixes)) {
if (!StringUtils.isNullOrEmpty(prefixes)) {
prefixesMap = QueryUtils.parsePrefixes(prefixes);
for (Entry<String, String> prefix : prefixesMap.entrySet()) {
distinctResourcesQuery.addPrefix(prefix.getKey(), prefix.getValue());
Expand All @@ -622,7 +622,7 @@ private SelectBuilder constructSparqlWhere(final String prefixes, final String w
//Setup where
WhereClause whereClause = null;
try {
if (!StringUtils.isEmpty(where)) {
if (!StringUtils.isNullOrEmpty(where)) {
whereClause = QueryUtils.parseWhere(where, prefixesMap);
List<SimpleTerm> parseChildren = whereClause.children();
for (SimpleTerm simpleTerm : parseChildren) {
Expand Down Expand Up @@ -672,7 +672,7 @@ private SelectBuilder constructSparqlWhere(final String prefixes, final String w

//Setup searchTerms
//Add a sparql filter "FILTER regex(?o, "<searchTerms>", "i")" to the distinctResourcesQuery
if (!StringUtils.isEmpty(searchTerms)) {
if (!StringUtils.isNullOrEmpty(searchTerms)) {
ExprFactory factory = new ExprFactory();
E_Regex regex = factory.regex(factory.str("?o"), searchTerms, "i");
distinctResourcesQuery.addFilter(regex);
Expand Down
Loading
Loading