Skip to content

Commit

Permalink
Merge pull request eXist-db#5246 from dizzzz/feature/improve_ext_code…
Browse files Browse the repository at this point in the history
…_again

[refactor] Mechanical code improvements extensions
  • Loading branch information
line-o authored Mar 19, 2024
2 parents 8f6d743 + 45cca76 commit bd7810f
Show file tree
Hide file tree
Showing 76 changed files with 711 additions and 1,303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
private Sequence extractMetadataFromLocalResource(final XmldbURI docUri) throws XPathException {
try(final LockedDocument lockedDoc = context.getBroker().getXMLResource(docUri, LockMode.READ_LOCK)) {

if (lockedDoc != null && lockedDoc.getDocument() instanceof BinaryDocument) {
final BinaryDocument binDoc = (BinaryDocument)lockedDoc.getDocument();
if (lockedDoc != null && lockedDoc.getDocument() instanceof BinaryDocument binDoc) {

final BrokerPool pool = context.getBroker().getBrokerPool();
final BlobStore blobStore = pool.getBlobStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ public EXistElement(final NodeValue element, final XQueryContext context) {
@Override
public Iterable<Attribute> attributes() {

return () -> new Iterator<Attribute>() {
return () -> new Iterator<>() {

private final NamedNodeMap attrs = element.getNode().getAttributes();
private final int length = attrs.getLength();
private int position = 0;

@Override
public boolean hasNext() {
return(position < length);
return (position < length);
}

@Override
public Attribute next() {
if(position >= length){
if (position >= length) {
throw new NoSuchElementException();
}

return new EXistAttribute((Attr)attrs.item(position++));
return new EXistAttribute((Attr) attrs.item(position++));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ private MissingModuleHint extractMissingModuleHint(final RestXqServiceCompilatio

MissingModuleHint missingModuleHint = null;

if(e.getCause() instanceof XPathException) {
final XPathException xpe = (XPathException)e.getCause();
if(e.getCause() instanceof XPathException xpe) {
if(xpe.getErrorCode() == ErrorCodes.XQST0059) {
final Sequence errorVals = xpe.getErrorVal();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ private Optional<EffectiveSubject> getEffectiveSubject(final CompiledXQuery xque
final Optional<EffectiveSubject> effectiveSubject;

final Source src = xquery.getContext().getSource();
if(src instanceof DBSource) {
final DBSource dbSrc = (DBSource)src;
if(src instanceof DBSource dbSrc) {
final Permission perm = dbSrc.getPermissions();

if(perm.isSetUid()) {
Expand Down Expand Up @@ -357,7 +356,7 @@ private <X> TypedValue<X> convertToType(final XQueryContext xqueryContext, final
throw new RestXqServiceException("TODO need to implement error code for problem with parameter conversion!: " + xpe.getMessage(), xpe);
}

return new TypedValue<X>() {
return new TypedValue<>() {

@Override
public org.exquery.xquery.Type getType() {
Expand All @@ -367,7 +366,7 @@ public org.exquery.xquery.Type getType() {

@Override
public X getValue() {
return (X)convertedValue;
return (X) convertedValue;
}
};
}
Expand All @@ -379,67 +378,23 @@ private org.exist.xquery.value.Sequence convertToExistSequence(final XQueryConte

final org.exquery.xquery.Type destinationType = TypeAdapter.toExQueryType(fnParameterType);

final Class destinationClass;

switch(fnParameterType) {

case Type.ITEM:
destinationClass = Item.class;
break;

case Type.DOCUMENT:
destinationClass = DocumentImpl.class; //TODO test this
break;

case Type.STRING:
destinationClass = StringValue.class;
break;

case Type.INT:
case Type.INTEGER:
destinationClass = IntegerValue.class;
break;

case Type.FLOAT:
destinationClass = FloatValue.class;
break;

case Type.DOUBLE:
destinationClass = DoubleValue.class;
break;

case Type.DECIMAL:
destinationClass = DecimalValue.class;
break;

case Type.DATE:
destinationClass = DateValue.class;
break;

case Type.DATE_TIME:
destinationClass = DateTimeValue.class;
break;

case Type.TIME:
destinationClass = TimeValue.class;
break;

case Type.QNAME:
destinationClass = QNameValue.class;
break;

case Type.ANY_URI:
destinationClass = AnyURIValue.class;
break;

case Type.BOOLEAN:
destinationClass = BooleanValue.class;
break;

default:
destinationClass = Item.class;
}

final Class destinationClass = switch (fnParameterType) {
case Type.ITEM -> Item.class;
case Type.DOCUMENT -> DocumentImpl.class; //TODO test this
case Type.STRING -> StringValue.class;
case Type.INT, Type.INTEGER -> IntegerValue.class;
case Type.FLOAT -> FloatValue.class;
case Type.DOUBLE -> DoubleValue.class;
case Type.DECIMAL -> DecimalValue.class;
case Type.DATE -> DateValue.class;
case Type.DATE_TIME -> DateTimeValue.class;
case Type.TIME -> TimeValue.class;
case Type.QNAME -> QNameValue.class;
case Type.ANY_URI -> AnyURIValue.class;
case Type.BOOLEAN -> BooleanValue.class;
default -> Item.class;
};

final TypedValue<? extends Item> val = convertToType(xqueryContext, argument.getArgumentName(), value, destinationType, destinationClass);

sequence.add(val.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void removeServices(final Iterable<RestXqService> services) {
private static <T, U> Iterable<U> mapIterable(final Iterable<T> input, final Function<T, U> mapper) {
return () -> {
final Iterator<T> it = input.iterator();
return new Iterator<U>() {
return new Iterator<>() {
@Override
public boolean hasNext() {
return it.hasNext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.exquery.restxq.RestXqServiceRegistry;
import org.exquery.restxq.impl.RestXqServiceRegistryImpl;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ private static void getDependencies(final XQueryContext xqyCtx, final Map<String
final Iterator<Module> itModule = xqyCtx.getModules();
while(itModule.hasNext()) {
final Module module = itModule.next();
if(module instanceof ExternalModule) {
final ExternalModule extModule = (ExternalModule)module;
if(module instanceof ExternalModule extModule) {
final Source source = extModule.getSource();
if(source instanceof DBSource) {
final String moduleUri = getDbUri(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,13 @@
class CardinalityAdapter {

public static Cardinality getCardinality(final org.exist.xquery.Cardinality cardinality) {
switch (cardinality) {
case EMPTY_SEQUENCE:
return Cardinality.ZERO;
case EXACTLY_ONE:
return Cardinality.ONE;
case _MANY:
return Cardinality.MANY;
case ZERO_OR_ONE:
return Cardinality.ZERO_OR_ONE;
case ONE_OR_MORE:
return Cardinality.ONE_OR_MORE;
case ZERO_OR_MORE:
return Cardinality.ZERO_OR_MORE;
}
return null;
return switch (cardinality) {
case EMPTY_SEQUENCE -> Cardinality.ZERO;
case EXACTLY_ONE -> Cardinality.ONE;
case _MANY -> Cardinality.MANY;
case ZERO_OR_ONE -> Cardinality.ZERO_OR_ONE;
case ONE_OR_MORE -> Cardinality.ONE_OR_MORE;
case ZERO_OR_MORE -> Cardinality.ZERO_OR_MORE;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ private Map<String, List<String>> extractFormFields(final InputStream in) throws
}

try {
key = java.net.URLDecoder.decode(pair.substring(0, pos), UTF_8.name());
val = java.net.URLDecoder.decode(pair.substring(pos + 1, pair.length()), UTF_8.name());
key = java.net.URLDecoder.decode(pair.substring(0, pos), UTF_8);
val = java.net.URLDecoder.decode(pair.substring(pos + 1, pair.length()), UTF_8);
} catch (final Exception e) {
throw new IllegalArgumentException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ public SequenceAdapter(final org.exist.xquery.value.Sequence sequence, @Nullable

@Override
public Iterator<TypedValue<Item>> iterator() {
return new Iterator<TypedValue<Item>>(){
return new Iterator<>() {

private SequenceIterator iterator;

private SequenceIterator getIterator() {
if(iterator == null) {
if (iterator == null) {
try {
iterator = sequence.iterate();
} catch(final XPathException xpe) {
iterator = sequence.iterate();
} catch (final XPathException xpe) {
LOG.error("Unable to extract the underlying Sequence Iterator: {}. Falling back to EMPTY_ITERATOR", xpe.getMessage(), xpe);

iterator = SequenceIterator.EMPTY_ITERATOR;
}
}
return iterator;
}

@Override
public boolean hasNext() {
return getIterator().hasNext();
Expand All @@ -98,16 +98,16 @@ public void remove() {
}

private TypedValue<Item> createTypedValue(final Item item) {
return new TypedValue<Item>(){
return new TypedValue<>() {
@Override
public Type getType() {
return TypeAdapter.toExQueryType(item.getType());
}

@Override
public Item getValue() {
if(item instanceof NodeProxy) {
return DomEnhancingNodeProxyAdapter.create((NodeProxy)item); //RESTXQ expects to find DOM Nodes not NodeProxys
if (item instanceof NodeProxy) {
return DomEnhancingNodeProxyAdapter.create((NodeProxy) item); //RESTXQ expects to find DOM Nodes not NodeProxys
} else {
return item;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ protected static Analyzer configureAnalyzer(Element config) throws DatabaseConfi
final Object cParamValues[] = new Object[cParams.size()];
for (int i = 0; i < cParams.size(); i++) {
KeyTypedValue<?> ktv = cParams.get(i);
cParamClasses[i] = ktv.getValueClass();
cParamValues[i] = ktv.getValue();
cParamClasses[i] = ktv.valueClass();
cParamValues[i] = ktv.value();
}

// Create new analyzer
Expand Down Expand Up @@ -474,7 +474,7 @@ static KeyTypedValue<?> getConstructorParameter(final Element param) throws Para
}
try {
final Integer n = Integer.parseInt(value);
parameter = new KeyTypedValue<>(name, n.intValue(), int.class);
parameter = new KeyTypedValue<>(name, n, int.class);
} catch (final NumberFormatException ex) {
LOG.error(String.format("Value %s could not be converted to an int. %s", value, ex.getMessage()));
}
Expand All @@ -493,7 +493,7 @@ static KeyTypedValue<?> getConstructorParameter(final Element param) throws Para
throw new ParameterException("The 'value' attribute must exist and must contain a boolean value.");
}
final Boolean b2 = Boolean.parseBoolean(value);
parameter = new KeyTypedValue<>(name, b2.booleanValue(), boolean.class);
parameter = new KeyTypedValue<>(name, b2, boolean.class);
break;

default:
Expand Down Expand Up @@ -588,32 +588,11 @@ private static CharArraySet getConstructorParameterCharArraySetValues(Element pa
}

/**
* CLass for containing the Triple : key (name), corresponding value and
* class type of value.
*/
static class KeyTypedValue<T> {

private final String key;
private final T value;
private final Class<T> valueClass;
* CLass for containing the Triple : key (name), corresponding value and
* class type of value.
*/
record KeyTypedValue<T>(String key, T value, Class<T> valueClass) {

public KeyTypedValue(final String key, final T value, final Class<T> valueClass) {
this.key = key;
this.value = value;
this.valueClass = valueClass;
}

public String getKey() {
return key;
}

public T getValue() {
return value;
}

public Class<T> getValueClass() {
return valueClass;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int startElement(QName name) {
}
if (isIgnoredNode(name)) {
stack++;
} else if (!isInlineNode(name) && buffer.length() > 0 && buffer.charAt(buffer.length() - 1) != ' ') {
} else if (!isInlineNode(name) && !buffer.isEmpty() && buffer.charAt(buffer.length() - 1) != ' ') {
// separate the current element's text from preceding text
buffer.append(' ');
return 1;
Expand All @@ -63,7 +63,7 @@ public int endElement(final QName name) {
}

public int beforeCharacters() {
if (addSpaceBeforeNext && buffer.length() > 0 && buffer.charAt(buffer.length() - 1) != ' ') {
if (addSpaceBeforeNext && !buffer.isEmpty() && buffer.charAt(buffer.length() - 1) != ' ') {
// separate the previous element's text from following text
buffer.append(' ');
addSpaceBeforeNext = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void createHierarchicalFacet(Document luceneDoc, Sequence seq) throws XP

@Override
protected void processText(CharSequence text, Document luceneDoc) {
if (text.length() > 0) {
if (!text.isEmpty()) {
luceneDoc.add(new FacetField(dimension, text.toString()));
}
}
Expand Down
Loading

0 comments on commit bd7810f

Please sign in to comment.