Repository: commons-rdf Updated Branches: refs/heads/fluent-parser 8fad253a6 -> 9c22589a6
Mutable/Immutable config Immutable using With* mini-classes for specializations thus immutable stays immutable Project: http://git-wip-us.apache.org/repos/asf/commons-rdf/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-rdf/commit/648f18b2 Tree: http://git-wip-us.apache.org/repos/asf/commons-rdf/tree/648f18b2 Diff: http://git-wip-us.apache.org/repos/asf/commons-rdf/diff/648f18b2 Branch: refs/heads/fluent-parser Commit: 648f18b298ef4540c244ab0af8274fad15bfa532 Parents: 8fad253 Author: Stian Soiland-Reyes <st...@apache.org> Authored: Mon Feb 19 08:57:29 2018 +0000 Committer: Stian Soiland-Reyes <st...@apache.org> Committed: Mon Feb 19 08:57:29 2018 +0000 ---------------------------------------------------------------------- .../rdf/api/io/AbstractParserBuilder.java | 10 +- .../apache/commons/rdf/api/io/AsyncImpl.java | 48 --- .../commons/rdf/api/io/MutableParserConfig.java | 119 +++++++ .../commons/rdf/api/io/NullParserConfig.java | 307 +++++++++++++++++++ .../apache/commons/rdf/api/io/ParserConfig.java | 43 ++- .../commons/rdf/api/io/ParserConfigImpl.java | 120 -------- .../rdf/api/io/SnapshotParserConfig.java | 29 ++ 7 files changed, 502 insertions(+), 174 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/648f18b2/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AbstractParserBuilder.java ---------------------------------------------------------------------- diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AbstractParserBuilder.java b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AbstractParserBuilder.java index 5bd27f1..03c6b93 100644 --- a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AbstractParserBuilder.java +++ b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AbstractParserBuilder.java @@ -48,14 +48,14 @@ public final class AbstractParserBuilder implements Cloneable, Serializable, Nee private static final ExecutorService DEFAULT_EXECUTOR = Executors.newCachedThreadPool(r -> new Thread(THEAD_GROUP, r)); public AbstractParserBuilder(RDF rdf) { - this.config = new ParserConfigImpl(rdf); + this.config = ParserConfig.mutable().withRDF(rdf); } @Override public AbstractParserBuilder clone() { try { AbstractParserBuilder c = (AbstractParserBuilder) super.clone(); - c.config = (ParserConfigImpl) config.clone(); + c.config = (MutableParserConfig) config.clone(); return c; } catch (CloneNotSupportedException e) { throw new IllegalStateException("AbstractParserBuilder was not Cloneable", e); @@ -63,7 +63,7 @@ public final class AbstractParserBuilder implements Cloneable, Serializable, Nee } private boolean mutable = false; - private ParserConfigImpl config; + private ParserConfig config; private ExecutorService executor = DEFAULT_EXECUTOR; @Override @@ -194,12 +194,12 @@ public final class AbstractParserBuilder implements Cloneable, Serializable, Nee @Override public Parsed parse() { // ensure immutable copy of config - ParserConfigImpl c = mutable(false).config; + MutableParserConfig c = mutable(false).config; Parser parser = getParserOrFail(c); return parser.parse(c); } - private Parser getParserOrFail(ParserConfigImpl c) { + private Parser getParserOrFail(MutableParserConfig c) { if (! c.rdf().isPresent()) { throw new IllegalStateException("ParserState has no RDF instance configured"); } http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/648f18b2/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AsyncImpl.java ---------------------------------------------------------------------- diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AsyncImpl.java b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AsyncImpl.java deleted file mode 100644 index 2c0c0d5..0000000 --- a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/AsyncImpl.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 org.apache.commons.rdf.api.io; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; - -import org.apache.commons.rdf.api.fluentparser.Async; - -public class AsyncImpl implements Async { - - public static final ThreadGroup threadGroup = new ThreadGroup("Commons RDF parsers"); - private static final ExecutorService threadpool = Executors.newCachedThreadPool(r -> new Thread(threadGroup, r)); - - @Override - public Async build() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Async option(Option option, Object value) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Future parseAsync() { - // TODO Auto-generated method stub - return null; - } - -} http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/648f18b2/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/MutableParserConfig.java ---------------------------------------------------------------------- diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/MutableParserConfig.java b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/MutableParserConfig.java new file mode 100644 index 0000000..7a43f74 --- /dev/null +++ b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/MutableParserConfig.java @@ -0,0 +1,119 @@ +/* + * 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 org.apache.commons.rdf.api.io; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutorService; + +import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.RDF; +import org.apache.commons.rdf.api.RDFSyntax; + +final class MutableParserConfig implements Cloneable, Serializable, ParserConfig { + private static final long serialVersionUID = 1L; + private RDF rdf = null; + private RDFSyntax syntax = null; + private IRI base = null; + @SuppressWarnings("rawtypes") + private ParserSource source = null; + @SuppressWarnings("rawtypes") + private ParserTarget target = null; + private final Map<Option, Object> options = new HashMap<>(); + private ExecutorService executor; + + public MutableParserConfig() { + } + + public MutableParserConfig(ParserConfig old) { + rdf = old.rdf().orElse(null); + syntax = old.syntax().orElse(null); + base = old.base().orElse(null); + source = old.source().orElse(null); + target = old.target().orElse(null); + options.putAll(old.options()); + } + + @Override + protected Object clone() { + return new MutableParserConfig(this); + } + + @Override + public Optional<ParserSource> source() { + return Optional.of(source); + } + + @Override + public Optional<IRI> base() { + return Optional.of(base); + } + + @Override + public Optional<ParserTarget> target() { + return Optional.of(target); + } + + @Override + public Optional<RDFSyntax> syntax() { + return Optional.of(syntax); + } + + @Override + public Optional<RDF> rdf() { + return Optional.of(rdf); + } + + @Override + public Map<Option, Object> options() { + return options; + } + + public ParserConfig withSyntax(RDFSyntax syntax) { + this.syntax = syntax; + return this; + } + + @SuppressWarnings("rawtypes") + public ParserConfig withSource(ParserSource source) { + this.source = source; + return this; + } + + public ParserConfig withTarget(ParserTarget target) { + this.target = target; + return this; + } + + public ParserConfig withRDF(RDF rdf) { + this.rdf = rdf; + return this; + } + + public ParserConfig withBase(IRI base) { + this.base = base; + return this; + } + + public <V> ParserConfig withOption(Option<V> o, V v) { + this.options.put(o, v); + return this; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/648f18b2/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java ---------------------------------------------------------------------- diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java new file mode 100644 index 0000000..e993949 --- /dev/null +++ b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/NullParserConfig.java @@ -0,0 +1,307 @@ +/* + * 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 org.apache.commons.rdf.api.io; + +import java.io.ObjectStreamException; +import java.io.Serializable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.ExecutorService; + +import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.RDF; +import org.apache.commons.rdf.api.RDFSyntax; +import org.apache.commons.rdf.api.io.ParserConfig.ImmutableParserConfig; + +class NullParserConfig implements ImmutableParserConfig, Serializable { + + private static final long serialVersionUID = 1L; + + @Override + public Optional<ParserSource> source() { + return Optional.empty(); + } + + @Override + public Optional<IRI> base() { + return Optional.empty(); + } + + @Override + public Optional<ParserTarget> target() { + return Optional.empty(); + } + + @Override + public Optional<RDFSyntax> syntax() { + return Optional.empty(); + } + + @Override + public Optional<RDF> rdf() { + return Optional.empty(); + } + + @Override + public Map<Option, Object> options() { + return Collections.emptyMap(); + } + + @Override + public ParserConfig withSyntax(RDFSyntax syntax) { + return new WithSyntax(this, syntax); + } + + @SuppressWarnings("rawtypes") + @Override + public ParserConfig withSource(ParserSource source) { + return new WithSource(this, source); + } + + @SuppressWarnings("rawtypes") + @Override + public ParserConfig withTarget(ParserTarget target) { + return new WithTarget(this, target); + } + + @Override + public ParserConfig withRDF(RDF rdf) { + return new WithRDF(this, rdf); + } + + @Override + public ParserConfig withBase(IRI base) { + return new WithBase(this, base); + } + + @Override + public <V> ParserConfig withOption(Option<V> o, V v) { + return new WithOptional(this, o, v); + } + + @SuppressWarnings("rawtypes") + final class WithOptional extends WithParent { + + private final Map<Option, Object> options; + + public <V> WithOptional(final ImmutableParserConfig parent, final Option<V> o, V v) { + super(parent); + this.options = new HashMap<>(parent.options()); + options.put(o, v); + } + + @Override + public Map<Option, Object> options() { + return options; + } + + } + + final class WithBase extends WithParent { + private final IRI base; + + WithBase(final ImmutableParserConfig parent, final IRI base) { + super(parent); + this.base = base; + } + + @Override + public Optional<IRI> base() { + return Optional.of(base); + } + } + + final class WithRDF extends WithParent { + private final RDF rdf; + + WithRDF(final ImmutableParserConfig parent, final RDF rdf) { + super(parent); + this.rdf = rdf; + } + + @Override + public Optional<RDF> rdf() { + return Optional.of(rdf); + } + } + + @SuppressWarnings("rawtypes") + final class WithTarget extends WithParent { + private final ParserTarget target; + + WithTarget(final ImmutableParserConfig parent, final ParserTarget target) { + super(parent); + this.target = target; + } + + @Override + public Optional<ParserTarget> target() { + return Optional.of(target); + } + } + + @SuppressWarnings("rawtypes") + public class WithSource extends WithParent { + private final ParserSource source; + + WithSource(ImmutableParserConfig parent, ParserSource source) { + super(parent); + this.source = source; + } + + @Override + public Optional<ParserSource> source() { + return Optional.of(source); + } + } + + static class WithSyntax extends WithParent { + private final RDFSyntax syntax; + + WithSyntax(ImmutableParserConfig parent, RDFSyntax syntax) { + super(parent); + this.syntax = syntax; + } + + @Override + public Optional<RDFSyntax> syntax() { + return Optional.of(syntax); + } + } + + abstract static class WithParent extends NullParserConfig implements ImmutableParserConfig { + private final ImmutableParserConfig parent; + + /** + * Override which object to use by Serializable, avoiding + * serialization of the whole WithParent tree. + * + * This method is protected so it will be invoked for all subclasses of + * WithParent. + * + * @return a {@link SnapshotParserConfig} + * @throws ObjectStreamException + */ + protected Object writeReplace() throws ObjectStreamException { + return new SnapshotParserConfig(this); + } + + WithParent(ImmutableParserConfig parserConfig) { + this.parent = Objects.requireNonNull(parserConfig); + } + + @SuppressWarnings("rawtypes") + @Override + public Optional<ParserSource> source() { + return parent.source(); + } + + @Override + public Optional<IRI> base() { + return parent.base(); + } + + @SuppressWarnings("rawtypes") + @Override + public Optional<ParserTarget> target() { + return parent.target(); + } + + @Override + public Optional<RDFSyntax> syntax() { + return parent.syntax(); + } + + @Override + public Optional<RDF> rdf() { + return parent.rdf(); + } + + @SuppressWarnings("rawtypes") + @Override + public Map<Option, Object> options() { + return parent.options(); + } + } + + @SuppressWarnings("rawtypes") + final static class SnapshotParserConfig extends NullParserConfig { + private static final long serialVersionUID = 1L; + private final RDF rdf; + private final RDFSyntax syntax; + private final IRI base; + private final ParserSource source; + private final ParserTarget target; + private final Map<Option, Object> options = new HashMap<>(); + private final ExecutorService executor; + + SnapshotParserConfig(ParserConfig old) { + this( + old.rdf().orElse(null), + old.syntax().orElse(null), + old.base().orElse(null), + old.source().orElse(null), + old.target().orElse(null), + old.options(), + null); + } + + SnapshotParserConfig(RDF rdf, RDFSyntax syntax, IRI base, ParserSource source, ParserTarget target, Map<Option, Object> options, + ExecutorService executor) { + this.rdf = rdf; + this.syntax = syntax; + this.base = base; + this.source = source; + this.target = target; + this.options.putAll(options); + this.executor = executor; + } + + @Override + public Optional<ParserSource> source() { + return Optional.of(source); + } + + @Override + public Optional<IRI> base() { + return Optional.of(base); + } + + @Override + public Optional<ParserTarget> target() { + return Optional.of(target); + } + + @Override + public Optional<RDFSyntax> syntax() { + return Optional.of(syntax); + } + + @Override + public Optional<RDF> rdf() { + return Optional.of(rdf); + } + + @Override + public Map<Option, Object> options() { + return options; + } + } +} http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/648f18b2/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfig.java ---------------------------------------------------------------------- diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfig.java b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfig.java index d18a268..b767dcc 100644 --- a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfig.java +++ b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfig.java @@ -16,14 +16,17 @@ */ package org.apache.commons.rdf.api.io; +import java.io.Serializable; import java.util.Map; import java.util.Optional; import org.apache.commons.rdf.api.IRI; import org.apache.commons.rdf.api.RDF; import org.apache.commons.rdf.api.RDFSyntax; +import org.apache.commons.rdf.api.io.NullParserConfig.SnapshotParserConfig; -public interface ParserConfig { +@SuppressWarnings("rawtypes") +interface ParserConfig { Optional<ParserSource> source(); Optional<IRI> base(); Optional<ParserTarget> target(); @@ -31,4 +34,42 @@ public interface ParserConfig { Optional<RDF> rdf(); Map<Option, Object> options(); + ParserConfig withSyntax(RDFSyntax syntax); + + ParserConfig withSource(ParserSource source); + + ParserConfig withTarget(ParserTarget target); + + ParserConfig withRDF(RDF rdf); + + ParserConfig withBase(IRI base); + + <V> ParserConfig withOption(Option<V> o, V v); + + static ParserConfig immutable() { + return new NullParserConfig(); + } + + static ParserConfig mutable() { + return new MutableParserConfig(); + } + + default ParserConfig asMutableConfig() { + if (this instanceof MutableParserConfig) { + return this; + } else { + return new MutableParserConfig(this); + } + } + + default ParserConfig asImmutableConfig() { + if (this instanceof ImmutableParserConfig) { + return this; + } else { + return new SnapshotParserConfig(this); + } + } + + interface ImmutableParserConfig extends ParserConfig, Serializable {} + } http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/648f18b2/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigImpl.java ---------------------------------------------------------------------- diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigImpl.java b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigImpl.java deleted file mode 100644 index 2062b6e..0000000 --- a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/ParserConfigImpl.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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 org.apache.commons.rdf.api.io; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ExecutorService; - -import org.apache.commons.rdf.api.IRI; -import org.apache.commons.rdf.api.RDF; -import org.apache.commons.rdf.api.RDFSyntax; - -public final class ParserConfigImpl implements Cloneable, Serializable, ParserConfig { - private static final long serialVersionUID = 1L; - private RDF rdf = null; - private RDFSyntax syntax = null; - private IRI base = null; - @SuppressWarnings("rawtypes") - private ParserSource source = null; - @SuppressWarnings("rawtypes") - private ParserTarget target = null; - private final Map<Option, Object> options = new HashMap<>(); - private ExecutorService executor; - - public ParserConfigImpl(RDF rdf) { - this.rdf = rdf; - } - - public ParserConfigImpl(ParserConfig old) { - rdf = old.rdf().orElse(null); - syntax = old.syntax().orElse(null); - base = old.base().orElse(null); - source = old.source().orElse(null); - target = old.target().orElse(null); - options.putAll(old.options()); - } - - @Override - protected Object clone() { - return new ParserConfigImpl(this); - } - - @Override - public Optional<ParserSource> source() { - return Optional.of(source); - } - - @Override - public Optional<IRI> base() { - return Optional.of(base); - } - - @Override - public Optional<ParserTarget> target() { - return Optional.of(target); - } - - @Override - public Optional<RDFSyntax> syntax() { - return Optional.of(syntax); - } - - @Override - public Optional<RDF> rdf() { - return Optional.of(rdf); - } - - @Override - public Map<Option, Object> options() { - return options; - } - - public ParserConfig withSyntax(RDFSyntax syntax) { - this.syntax = syntax; - return this; - } - - @SuppressWarnings("rawtypes") - public ParserConfig withSource(ParserSource source) { - this.source = source; - return this; - } - - public ParserConfig withTarget(ParserTarget target) { - this.target = target; - return this; - } - - public ParserConfig withRDF(RDF rdf) { - this.rdf = rdf; - return this; - } - - public ParserConfig withBase(IRI base) { - this.base = base; - return this; - } - - public <V> ParserConfig withOption(Option<V> o, V v) { - this.options.put(o, v); - return this; - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/648f18b2/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/SnapshotParserConfig.java ---------------------------------------------------------------------- diff --git a/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/SnapshotParserConfig.java b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/SnapshotParserConfig.java new file mode 100644 index 0000000..47e7ce9 --- /dev/null +++ b/commons-rdf-api/src/main/java/org/apache/commons/rdf/api/io/SnapshotParserConfig.java @@ -0,0 +1,29 @@ +/* + * 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 org.apache.commons.rdf.api.io; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutorService; + +import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.RDF; +import org.apache.commons.rdf.api.RDFSyntax; +import org.apache.commons.rdf.api.io.ParserConfig.ImmutableParserConfig; +