[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16509539#comment-16509539 ] ASF GitHub Bot commented on JENA-1559: -- Github user asfgit closed the pull request at: https://github.com/apache/jena/pull/431 > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16509535#comment-16509535 ] ASF subversion and git services commented on JENA-1559: --- Commit 2377f47e72e499e545f6d1154e287bc2cc0b5acc in jena's branch refs/heads/master from [~an...@apache.org] [ https://git-wip-us.apache.org/repos/asf?p=jena.git;h=2377f47 ] JENA-1559: PrefixMappingBase - algorithm and storage separation > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16509537#comment-16509537 ] ASF subversion and git services commented on JENA-1559: --- Commit 481e5894f29c75d7fdf9edadc027374b58f863ee in jena's branch refs/heads/master from [~an...@apache.org] [ https://git-wip-us.apache.org/repos/asf?p=jena.git;h=481e589 ] JENA-1559: Merge commit 'refs/pull/431/head' of https://github.com/apache/jena This closes #431. > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16508058#comment-16508058 ] ASF GitHub Bot commented on JENA-1559: -- Github user afs commented on a diff in the pull request: https://github.com/apache/jena/pull/431#discussion_r194405024 --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/graph/PrefixMappingBase.java --- @@ -0,0 +1,277 @@ +/* + * 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.jena.sparql.graph; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.StringJoiner; +import java.util.function.BiConsumer; + +import org.apache.jena.ext.xerces.util.XMLChar; +import org.apache.jena.shared.PrefixMapping; +import org.apache.jena.util.SplitIRI; + +/** + * Framework for implementing {@link PrefixMapping}. It is stateless (unlike + * {@code PrefixMappingImpl}) and implements the contract of {@link PrefixMapping}, + * providing the key algorithm and delegating storage to the subclasses. + * + * Reverse mapping, looking up a URI to find a prefix is complex. There may be several + * possibilities. Applications should not reliy on every implementat being consistent, + * espeically when writing data when there has to be a choice on on prefix to use to + * shorten a URI. + * + */ +public abstract class PrefixMappingBase implements PrefixMapping { +/* Reverse mappings. + * The strict contract of PrefixMapping requires a separate reversemapping to be storoed and manipuated + * which in turn adds complexity to the storage implementations. + * However, applications removing prefixes is unusual so we end up with a lot of complexity with little value. + * + * Beware of the details of removing a mapping when there is another to the same URI. + * If we had: + * + * Add (pref1, U) + * Add (pref2, U) + * + * so that {@code U} reverse maps ({@link #getNsURIPrefix}) to {@code pref2} (it was + * done second) then + * + * Remove (pref2) + * + * it causes {@code U} to reverse map to {@code pref1}. + * + * This feature is quite a burden on implementations and should be regarded as "legacy" - + * an implementation may not support this complex effect. + * + * PrefixMappingMem does. + * Database backed ones typically don't. + */ + +protected PrefixMappingBase() {} + +// The storage operations of an implementation. + +/** Add prefix */ +abstract protected void add(String prefix, String uri); + +/** Remove prefix. */ +abstract protected void remove(String prefix); + +/** Clear all mappings */ +abstract protected void clear(); + +abstract protected boolean isEmpty(); + +abstract protected int size(); + +/** Return the URI that the prefix maps to. */ +abstract protected String prefixToUri(String prefix); + +/** Return a prefix that maps to the URI. + * There may be several; the answer is any one of them. + */ +abstract protected String uriToPrefix(String uri); + +/** Return as a map. This map is only used within this class. + * It can be as efficient as possible. + * It will not be modifed. + * It wil not be retuned to a caller of {@code PrefixMappingBase}. + */ +abstract protected Map asMap(); + +/** + * Return as a map. The map return is not connected to the prefix mapping implementation, + * does not reflect subsequent prefix mapping changes. + */ +abstract protected Map asMapCopy(); + +/** Apply the {@link BiConsumer} to each (prefix, uri) pair. */ +abstract protected void
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16508025#comment-16508025 ] ASF GitHub Bot commented on JENA-1559: -- Github user ajs6f commented on a diff in the pull request: https://github.com/apache/jena/pull/431#discussion_r194399107 --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/graph/PrefixMappingOver.java --- @@ -0,0 +1,97 @@ +/* + * 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.jena.sparql.graph; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.function.BiConsumer; + +import org.apache.jena.iri.IRI; +import org.apache.jena.riot.system.PrefixMap; + +public class PrefixMappingOver extends PrefixMappingBase { --- End diff -- Can't speak for @rvesse, but "Adaptor" is even better to my eye. > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16508013#comment-16508013 ] ASF GitHub Bot commented on JENA-1559: -- Github user afs commented on a diff in the pull request: https://github.com/apache/jena/pull/431#discussion_r194398068 --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/graph/PrefixMappingOver.java --- @@ -0,0 +1,97 @@ +/* + * 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.jena.sparql.graph; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.function.BiConsumer; + +import org.apache.jena.iri.IRI; +import org.apache.jena.riot.system.PrefixMap; + +public class PrefixMappingOver extends PrefixMappingBase { --- End diff -- Its real name is `PrefixMappingOverPrefixMap` ... seemed a bit long and confusing. It does not seem to be a decorator. Would "Adapter" be OK? > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16507906#comment-16507906 ] ASF GitHub Bot commented on JENA-1559: -- Github user ajs6f commented on a diff in the pull request: https://github.com/apache/jena/pull/431#discussion_r194364163 --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/graph/PrefixMappingOver.java --- @@ -0,0 +1,97 @@ +/* + * 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.jena.sparql.graph; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.function.BiConsumer; + +import org.apache.jena.iri.IRI; +import org.apache.jena.riot.system.PrefixMap; + +public class PrefixMappingOver extends PrefixMappingBase { --- End diff -- +1 > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16507839#comment-16507839 ] ASF GitHub Bot commented on JENA-1559: -- Github user rvesse commented on a diff in the pull request: https://github.com/apache/jena/pull/431#discussion_r194339397 --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/graph/PrefixMappingOver.java --- @@ -0,0 +1,97 @@ +/* + * 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.jena.sparql.graph; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.function.BiConsumer; + +import org.apache.jena.iri.IRI; +import org.apache.jena.riot.system.PrefixMap; + +public class PrefixMappingOver extends PrefixMappingBase { --- End diff -- Would `PrefixMappingDecorator` be a more descriptive name? > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16507837#comment-16507837 ] ASF GitHub Bot commented on JENA-1559: -- Github user rvesse commented on a diff in the pull request: https://github.com/apache/jena/pull/431#discussion_r194338419 --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/graph/PrefixMappingBase.java --- @@ -0,0 +1,277 @@ +/* + * 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.jena.sparql.graph; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.StringJoiner; +import java.util.function.BiConsumer; + +import org.apache.jena.ext.xerces.util.XMLChar; +import org.apache.jena.shared.PrefixMapping; +import org.apache.jena.util.SplitIRI; + +/** + * Framework for implementing {@link PrefixMapping}. It is stateless (unlike + * {@code PrefixMappingImpl}) and implements the contract of {@link PrefixMapping}, + * providing the key algorithm and delegating storage to the subclasses. + * + * Reverse mapping, looking up a URI to find a prefix is complex. There may be several + * possibilities. Applications should not reliy on every implementat being consistent, + * espeically when writing data when there has to be a choice on on prefix to use to + * shorten a URI. + * + */ +public abstract class PrefixMappingBase implements PrefixMapping { +/* Reverse mappings. + * The strict contract of PrefixMapping requires a separate reversemapping to be storoed and manipuated --- End diff -- Typos: - Missing space in `reverse mapping` - `storoed` - `manipuated` > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16507836#comment-16507836 ] ASF GitHub Bot commented on JENA-1559: -- Github user rvesse commented on a diff in the pull request: https://github.com/apache/jena/pull/431#discussion_r194338271 --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/graph/PrefixMappingBase.java --- @@ -0,0 +1,277 @@ +/* + * 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.jena.sparql.graph; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.StringJoiner; +import java.util.function.BiConsumer; + +import org.apache.jena.ext.xerces.util.XMLChar; +import org.apache.jena.shared.PrefixMapping; +import org.apache.jena.util.SplitIRI; + +/** + * Framework for implementing {@link PrefixMapping}. It is stateless (unlike + * {@code PrefixMappingImpl}) and implements the contract of {@link PrefixMapping}, + * providing the key algorithm and delegating storage to the subclasses. + * + * Reverse mapping, looking up a URI to find a prefix is complex. There may be several + * possibilities. Applications should not reliy on every implementat being consistent, --- End diff -- Typos - `implementat` this line - `espeically` on the following line > Avoid unnecessary prefix loading in GraphPrefixesProjection > > > Key: JENA-1559 > URL: https://issues.apache.org/jira/browse/JENA-1559 > Project: Apache Jena > Issue Type: Task >Affects Versions: Jena 3.7.0 >Reporter: Andy Seaborne >Assignee: Andy Seaborne >Priority: Major > > {{GraphPrefixesProjection}} loads all prefixes from the database (JENA-81). > It was done that way because {{PrefixMappingImpl}} has internal > datastructures that are not passing through lookup misses. > Load as needed and rework the way the prefix implementation to cleanly > separate algorithms from storage. > Leave {{PrefixMappingImpl}} untouched - its name and details are too > well-known. > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)
[jira] [Commented] (JENA-1559) Avoid unnecessary prefix loading in GraphPrefixesProjection
[ https://issues.apache.org/jira/browse/JENA-1559?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16507838#comment-16507838 ] ASF GitHub Bot commented on JENA-1559: -- Github user rvesse commented on a diff in the pull request: https://github.com/apache/jena/pull/431#discussion_r194338879 --- Diff: jena-arq/src/main/java/org/apache/jena/sparql/graph/PrefixMappingBase.java --- @@ -0,0 +1,277 @@ +/* + * 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.jena.sparql.graph; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.StringJoiner; +import java.util.function.BiConsumer; + +import org.apache.jena.ext.xerces.util.XMLChar; +import org.apache.jena.shared.PrefixMapping; +import org.apache.jena.util.SplitIRI; + +/** + * Framework for implementing {@link PrefixMapping}. It is stateless (unlike + * {@code PrefixMappingImpl}) and implements the contract of {@link PrefixMapping}, + * providing the key algorithm and delegating storage to the subclasses. + * + * Reverse mapping, looking up a URI to find a prefix is complex. There may be several + * possibilities. Applications should not reliy on every implementat being consistent, + * espeically when writing data when there has to be a choice on on prefix to use to + * shorten a URI. + * + */ +public abstract class PrefixMappingBase implements PrefixMapping { +/* Reverse mappings. + * The strict contract of PrefixMapping requires a separate reversemapping to be storoed and manipuated + * which in turn adds complexity to the storage implementations. + * However, applications removing prefixes is unusual so we end up with a lot of complexity with little value. + * + * Beware of the details of removing a mapping when there is another to the same URI. + * If we had: + * + * Add (pref1, U) + * Add (pref2, U) + * + * so that {@code U} reverse maps ({@link #getNsURIPrefix}) to {@code pref2} (it was + * done second) then + * + * Remove (pref2) + * + * it causes {@code U} to reverse map to {@code pref1}. + * + * This feature is quite a burden on implementations and should be regarded as "legacy" - + * an implementation may not support this complex effect. + * + * PrefixMappingMem does. + * Database backed ones typically don't. + */ + +protected PrefixMappingBase() {} + +// The storage operations of an implementation. + +/** Add prefix */ +abstract protected void add(String prefix, String uri); + +/** Remove prefix. */ +abstract protected void remove(String prefix); + +/** Clear all mappings */ +abstract protected void clear(); + +abstract protected boolean isEmpty(); + +abstract protected int size(); + +/** Return the URI that the prefix maps to. */ +abstract protected String prefixToUri(String prefix); + +/** Return a prefix that maps to the URI. + * There may be several; the answer is any one of them. + */ +abstract protected String uriToPrefix(String uri); + +/** Return as a map. This map is only used within this class. + * It can be as efficient as possible. + * It will not be modifed. + * It wil not be retuned to a caller of {@code PrefixMappingBase}. + */ +abstract protected Map asMap(); + +/** + * Return as a map. The map return is not connected to the prefix mapping implementation, + * does not reflect subsequent prefix mapping changes. + */ +abstract protected Map asMapCopy(); + +/** Apply the {@link BiConsumer} to each (prefix, uri) pair. */ +abstract protected vo