001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.relations;
016
017import java.util.ArrayList;
018import java.util.List;
019
020public abstract class AbstractRelation implements Relation {
021
022    public List<Relate> relates;
023
024    private Predicate predicate;
025
026    protected AbstractRelation(Predicate predicate) {
027        this.predicate = predicate;
028        relates = new ArrayList<Relate>();
029    }
030
031    public abstract RelationTypeFamily getRelationTypeFamily();
032
033    public Relate getRelate(Integer position) {
034        if (position > relates.size()) {
035            // TODO throw exception;
036        }
037        return relates.get(position - 1);
038    }
039
040    public String hash() {
041        List<String> r = new ArrayList<String>();
042        for (Relate relate : relates) {
043            r.add(relate.hash());
044        }
045        return String.format(predicate.hash().replace("_", "%s"), r.toArray());
046    }
047
048    @Override
049    public String toString() {
050        return "Relation {'" + hash() + "'}";
051    }
052
053    public Predicate getPredicate() {
054        return predicate;
055    }
056
057    public final void setPredicate(Predicate predicate) {
058        this.predicate = predicate;
059    }
060
061    public boolean hasPredicate(Predicate predicate) {
062        return this.predicate.hash().equals(predicate.hash());
063    }
064
065    public List<Relate> getRelates() {
066        return relates;
067    }
068
069    public void setRelates(List<Relate> relates) {
070        this.relates = relates;
071    }
072
073}