Skip to content

Commit

Permalink
Merge pull request #49 from filip26/patch/iron
Browse files Browse the repository at this point in the history
Object to Compacted Form Writer
  • Loading branch information
filip26 authored Oct 31, 2024
2 parents 3e095cb + 14aa23a commit 8132301
Show file tree
Hide file tree
Showing 102 changed files with 2,569 additions and 1,435 deletions.
24 changes: 3 additions & 21 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

<groupId>com.apicatalog</groupId>
<artifactId>linked-tree</artifactId>
<version>0.0.13-SNAPSHOT</version>
<version>0.0.68-SNAPSHOT</version>

<packaging>jar</packaging>

<name>Linked Tree</name>

<description>
An abstract data model to process Linked Data
An abstract data model to process Linked Data powered by annotations to
enable seamless object mapping and processing.
</description>

<licenses>
Expand Down Expand Up @@ -55,7 +56,6 @@
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<envSources>src/main/java17</envSources>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand Down Expand Up @@ -94,24 +94,6 @@
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${envSources}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand Down
5 changes: 2 additions & 3 deletions java/src/main/java/com/apicatalog/linkedtree/Linkable.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ public interface Linkable {

static Method method() {
try {
return Linkable.class.getMethod("ld");
return Linkable.class.getDeclaredMethod("ld");
} catch (NoSuchMethodException | SecurityException e) {
// does not happen
throw new IllegalStateException(e);
}
}

LinkedNode ld();

LinkedNode ld();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public interface LinkedContainer extends LinkedNode, Iterable<LinkedNode> {
public enum ContainerType {
OrderedList,
UnorderedSet,
/**
* The first node is root node, the rest is unordered set
*/
Tree
};

Expand Down Expand Up @@ -39,7 +42,7 @@ default Collection<ProcessingInstruction> pi(int processingOrder) {
}
return Collections.emptyList();
}

@Override
default boolean isContainer() {
return true;
Expand Down Expand Up @@ -75,14 +78,13 @@ default <T> T materialize(Class<T> clazz) throws NodeAdapterError {
}

if (single.isFragment()) {
//FIXME if (single.asFragment().id() != null
// && single.asFragment().id().target() != null
// ) {
// return single.ld().asFragment().id().target().type().materialize(clazz);
// }
if (single.asFragment().id() != null
&& single.asFragment().id().target() != null) {
return single.asFragment().id().target().type().materialize(clazz);
}
return single.asFragment().type().materialize(clazz);
}

return (T) single;
}

Expand Down
37 changes: 23 additions & 14 deletions java/src/main/java/com/apicatalog/linkedtree/LinkedFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface LinkedFragment extends LinkedNode {
LinkedContainer container(String term);

@SuppressWarnings("unchecked")
default <T extends Linkable> T materialize(String term, Class<T> clazz) throws InvalidSelector, NodeAdapterError {
default <T> T materialize(String term, Class<T> clazz) throws InvalidSelector, NodeAdapterError {

Objects.requireNonNull(clazz);

Expand Down Expand Up @@ -63,11 +63,10 @@ default <T extends Linkable> T materialize(String term, Class<T> clazz) throws I
return node.asFragment().type().materialize(clazz);
}

if (node.isLiteral()
&& clazz.isInstance(node.asLiteral().cast())) {

return node.asLiteral().cast(clazz);
if (node.isLiteral() && clazz.isInstance(node)) {
return clazz.cast(node.asLiteral());
}

throw new InvalidSelector(term);

} catch (ClassCastException e) {
Expand All @@ -88,6 +87,16 @@ default LinkedNode node(String term) throws InvalidSelector {
return container.node();
}

/**
* A {@link LinkedTree} instance to which the {@link LinkedNode} belongs to.
*
* Please note a tree instance can have a root if is a child node of another
* tree instance.
*
* @return an instance or <code>null</code> if the node is a root
*/
LinkedTree root();

// default <R> R single(String term, LinkableMapper<R> mapper) throws DocumentError {
//
// Objects.requireNonNull(term);
Expand Down Expand Up @@ -160,15 +169,15 @@ default <R> R fragment(String term, Class<R> clazz, NodeAdapter<LinkedNode, R> m
// }
}

default <T extends Linkable> T literal(String term, Class<T> clazz) throws InvalidSelector {
default <T> T literal(String term, Class<T> clazz) throws InvalidSelector {
return literal(term, clazz, Function.identity());
}

default <T extends Linkable, R> R literal(String term, Class<T> clazz, Function<T, R> mapper) throws InvalidSelector {
default <T, R> R literal(String term, Class<T> clazz, Function<T, R> mapper) throws InvalidSelector {
return literal(term, clazz, mapper, null);
}

default <T extends Linkable, R> R literal(String term, Class<T> clazz, Function<T, R> mapper, R defaultValue) throws InvalidSelector {
default <T, R> R literal(String term, Class<T> clazz, Function<T, R> mapper, R defaultValue) throws InvalidSelector {

Objects.requireNonNull(mapper);

Expand Down Expand Up @@ -276,17 +285,17 @@ default <T> Collection<T> collection(

if (clazz.isInstance(node)) {
collection.add((T) node);
} else if (node.isFragment()

} else if (node.isFragment()
&& node.asFragment().id() != null
&& node.asFragment().id().target().type().isAdaptableTo(clazz)) {
&& node.asFragment().id().target().type().isAdaptableTo(clazz)) {
collection.add(node.asFragment().id().target().type().materialize(clazz));

} else if (node.isFragment() && node.asFragment().type().isAdaptableTo(clazz)) {
collection.add(node.asFragment().type().materialize(clazz));
} else if (node.isLiteral() && clazz.isInstance(node.asLiteral().cast())) {
collection.add(node.asLiteral().cast(clazz));

} else if (node.isLiteral() && clazz.isInstance(node.asLiteral())) {
collection.add(clazz.cast(node.asLiteral()));

} else if (unmapped != null) {
if (node.isFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,4 @@ default LinkedLiteral asLiteral() {
* @return an absolute IRI, never <code>null</code>
*/
String datatype();

@SuppressWarnings("unchecked")
default <T> T cast(Class<T> clazz) {
return (T) this;
}

default Linkable cast() {
return this;
}
}
17 changes: 1 addition & 16 deletions java/src/main/java/com/apicatalog/linkedtree/LinkedNode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.apicatalog.linkedtree;

public interface LinkedNode extends Linkable {
public interface LinkedNode {

default boolean isTree() {
return false;
Expand Down Expand Up @@ -39,19 +39,4 @@ default LinkedContainer asContainer() {

@Override
int hashCode();

/**
* A {@link LinkedTree} instance to which the {@link LinkedNode} belongs to.
*
* Please note a tree instance can have a root if is a child node of another
* tree instance.
*
* @return an instance or <code>null</code> if the node is a root
*/
LinkedTree root();

@Override
default LinkedNode ld() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ protected LinkedNode clone(LinkedNode source) {
}
return new ImmutableLiteral(
source.asLiteral().lexicalValue(),
source.asLiteral().datatype(),
root);
source.asLiteral().datatype());
}
throw new IllegalStateException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
import com.apicatalog.linkedtree.LinkedNode;
import com.apicatalog.linkedtree.LinkedTree;
import com.apicatalog.linkedtree.fragment.GenericFragment;
import com.apicatalog.linkedtree.lang.ImmutableLangString;
import com.apicatalog.linkedtree.lang.LangString;
import com.apicatalog.linkedtree.link.Link;
import com.apicatalog.linkedtree.link.MutableLink;
import com.apicatalog.linkedtree.literal.ImmutableLiteral;
import com.apicatalog.linkedtree.pi.ProcessingInstruction;
import com.apicatalog.linkedtree.primitive.GenericContainer;
import com.apicatalog.linkedtree.primitive.GenericTree;
Expand All @@ -37,10 +34,10 @@ public GenericTreeCompiler() {
this.nodeStack = null;
this.clonedTrees = null;
this.nodeSelector = null;
nodeStack = new Stack<>();
clonedTrees = new Stack<>();
nodeSelector = (node, indexOrder, indexTerm, depth) -> TraversalPolicy.Accept;

nodeStack = new Stack<>();
clonedTrees = new Stack<>();
nodeSelector = (node, indexOrder, indexTerm, depth) -> TraversalPolicy.Accept;
}

@Override
Expand Down Expand Up @@ -108,10 +105,10 @@ protected LinkedNode clone(LinkedNode source) {
if (source.isTree()) {

var tree = GenericTree.of(
source.asTree(),
source.asTree(),
cloneLink(source.asTree().id()),
root);

clonedTrees.push(tree);

return tree;
Expand All @@ -133,7 +130,7 @@ protected LinkedNode clone(LinkedNode source) {
cloneLink(source.asFragment().id()),

types,

new LinkedHashMap<>(source.asFragment().terms().size()),
root);

Expand All @@ -144,17 +141,18 @@ protected LinkedNode clone(LinkedNode source) {
return fragment;

} else if (source.isLiteral()) {
if (source.asLiteral() instanceof LangString langString) {
return new ImmutableLangString(
langString.lexicalValue(),
langString.language(),
langString.direction(),
root);
}
return new ImmutableLiteral(
source.asLiteral().lexicalValue(),
source.asLiteral().datatype(),
root);
// literals are immutable
return source;
// if (source.asLiteral() instanceof LangString langString) {
// return new ImmutableLangString(
// langString.lexicalValue(),
// langString.language(),
// langString.direction(),
// root);
// }
// return new ImmutableLiteral(
// source.asLiteral().lexicalValue(),
// source.asLiteral().datatype());
}
throw new IllegalStateException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Stack;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -366,6 +367,7 @@ private GenericFragment mutableFragment(

protected TreeBuilder<T> literal(
LinkedLiteral literal) {
Objects.requireNonNull(literal);
nodeStack.push(literal);
return this;
}
Expand All @@ -385,7 +387,7 @@ protected TreeBuilder<T> immutableLiteral(
String datatype,
Collection<ProcessingInstruction> ops) {
pi(ops);
nodeStack.push(new ImmutableLiteral(value, datatype, root()));
nodeStack.push(new ImmutableLiteral(value, datatype));
return this;
}

Expand Down
Loading

0 comments on commit 8132301

Please sign in to comment.