001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Jun 4, 2010
021 * Author: Jianjiong Gao
022 *
023 */
024
025package org.biojava.nbio.protmod;
026
027import java.util.Collections;
028import java.util.HashSet;
029import java.util.List;
030import java.util.Set;
031
032/**
033 *
034 * @author Jianjiong Gao
035 * @since 3.0
036 */
037public class ModificationConditionImpl implements ModificationCondition {
038        private final List<Component> components;
039        private final List<ModificationLinkage> linkages;
040
041        public ModificationConditionImpl(final List<Component> components,
042                        final List<ModificationLinkage> linkages) {
043
044                if ( components == null)
045                        throw new IllegalArgumentException("Can not create ModificationCondition, components == null!");
046
047                if ( components.isEmpty())
048                        throw new IllegalArgumentException("Can not create ModificationCondition, components is empty!");
049
050
051                if (components.size() > 1) {
052                        Set<Integer> indices = new HashSet<Integer>();
053                        for (ModificationLinkage linkage : linkages) {
054                                indices.add(linkage.getIndexOfComponent1());
055                                indices.add(linkage.getIndexOfComponent2());
056                        }
057
058                        // TODO: a more comprehensive check would be checking whether
059                        // all components are connected
060                        if (indices.size()!=components.size()) {
061                                throw new IllegalStateException("All components " +
062                                                "have to be linked. indices.size:" + indices.size() + " components size:" + components.size()); // TODO: is this true?
063                        }
064                }
065
066                this.components = Collections.unmodifiableList(components);
067                if (linkages==null) {
068                        this.linkages = Collections.emptyList();
069                } else {
070                        this.linkages = Collections.unmodifiableList(linkages);
071                }
072        }
073
074        /**
075         *
076         * {@inheritDoc}}
077         */
078        @Override
079        public List<Component> getComponents() {
080                return components;
081        }
082
083        /**
084         *
085         * {@inheritDoc}}
086         */
087        @Override
088        public List<ModificationLinkage> getLinkages() {
089                return linkages;
090        }
091
092        /**
093         *
094         */
095        @Override
096        public String toString() {
097                StringBuilder sb = new StringBuilder();
098                sb.append("Components:");
099                for (Component comp : components) {
100                        sb.append(comp).append(";");
101                }
102                sb.deleteCharAt(sb.length()-1);
103
104                if (!linkages.isEmpty()) {
105                        sb.append("\nLinkages:");
106                        for (ModificationLinkage link : linkages) {
107                                sb.append(link).append(";");
108                        }
109                        sb.deleteCharAt(sb.length()-1);
110                }
111
112                return sb.toString();
113        }
114}