001/**
002 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
003 *   This file is part of the LDP4j Project:
004 *     http://www.ldp4j.org/
005 *
006 *   Center for Open Middleware
007 *     http://www.centeropenmiddleware.com/
008 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
009 *   Copyright (C) 2014 Center for Open Middleware.
010 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
011 *   Licensed under the Apache License, Version 2.0 (the "License");
012 *   you may not use this file except in compliance with the License.
013 *   You may obtain a copy of the License at
014 *
015 *             http://www.apache.org/licenses/LICENSE-2.0
016 *
017 *   Unless required by applicable law or agreed to in writing, software
018 *   distributed under the License is distributed on an "AS IS" BASIS,
019 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 *   See the License for the specific language governing permissions and
021 *   limitations under the License.
022 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
023 *   Artifact    : org.ldp4j.framework:ldp4j-application-api:0.2.0
024 *   Bundle      : ldp4j-application-api-0.2.0.jar
025 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
026 */
027package org.ldp4j.application.data;
028
029import java.net.URI;
030
031public final class Individuals {
032
033  private static class ValueReplicator implements ValueVisitor {
034
035    private final IndividualFinder finder;
036    private Value result=null;
037
038    ValueReplicator(DataSet dataSet) {
039      this.finder=new IndividualFinder(dataSet);
040    }
041
042    Value replicate(Value value) {
043      value.accept(this);
044      return this.result;
045    }
046
047    @Override
048    public void visitLiteral(Literal<?> value) {
049      this.result=value;
050    }
051
052    @Override
053    public void visitIndividual(Individual<?,?> value) {
054      this.result=this.finder.findOrCreate(value);
055    }
056
057  }
058
059  private Individuals() {
060  }
061
062  public static void merge(Individual<?,?> source, Individual<?,?> target) {
063    ValueReplicator replicator=new ValueReplicator(target.dataSet());
064    for(Property property:source.properties()) {
065      for(Value value:property) {
066        target.addValue(property.predicate(),replicator.replicate(value));
067      }
068    }
069  }
070
071  public static void remove(Individual<?,?> source, final Individual<?,?> target) {
072    for(Property property:source.properties()) {
073      final URI propertyId=property.predicate();
074      ValueVisitor visitor = new ValueVisitor(){
075        @Override
076        public void visitLiteral(Literal<?> value) {
077          target.removeValue(propertyId, value);
078        }
079        @Override
080        public void visitIndividual(Individual<?,?> value) {
081          Individual<?, ?> cValue = target.dataSet().individualOfId(value.id());
082          if(cValue!=null) {
083            target.removeValue(propertyId, cValue);
084          }
085        }
086      };
087      for(Value value:property) {
088        value.accept(visitor);
089      }
090    }
091  }
092
093
094
095}