001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.relations;
023
024import java.util.ArrayList;
025import java.util.List;
026
027public abstract class AbstractRelation implements Relation {
028
029    public List<Relate> relates;
030
031    private Predicate predicate;
032
033    protected AbstractRelation(Predicate predicate) {
034        this.predicate = predicate;
035        relates = new ArrayList<Relate>();
036    }
037
038    public abstract RelationTypeFamily getRelationTypeFamily();
039
040    public Relate getRelate(Integer position) {
041        if (position > relates.size()) {
042            // TODO throw exception;
043        }
044        return relates.get(position - 1);
045    }
046
047    public String hash() {
048        List<String> r = new ArrayList<String>();
049        for (Relate relate : relates) {
050            r.add(relate.hash());
051        }
052        return String.format(predicate.hash().replace("_", "%s"), r.toArray());
053    }
054
055    @Override
056    public String toString() {
057        return "Relation {'" + hash() + "'}";
058    }
059
060    public Predicate getPredicate() {
061        return predicate;
062    }
063
064    public final void setPredicate(Predicate predicate) {
065        this.predicate = predicate;
066    }
067
068    public boolean hasPredicate(Predicate predicate) {
069        return this.predicate.hash().equals(predicate.hash());
070    }
071
072    public List<Relate> getRelates() {
073        return relates;
074    }
075
076    public void setRelates(List<Relate> relates) {
077        this.relates = relates;
078    }
079
080}