001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.property;
025    
026    import java.util.ArrayList;
027    import java.util.Collections;
028    import java.util.HashMap;
029    import java.util.List;
030    import java.util.Map;
031    import org.jboss.dna.graph.Location;
032    
033    /**
034     * @author Randall Hauch
035     */
036    public class ReferentialIntegrityException extends RuntimeException {
037    
038        /**
039         */
040        private static final long serialVersionUID = -3703984046286975978L;
041    
042        private final Map<Location, List<Reference>> invalidReferences;
043    
044        /**
045         * @param location the location of the node containing the bad reference(s)
046         * @param invalidReferences the invalid references
047         */
048        public ReferentialIntegrityException( Location location,
049                                              Reference... invalidReferences ) {
050            this.invalidReferences = new HashMap<Location, List<Reference>>();
051            List<Reference> invalidRefList = null;
052            if (invalidReferences == null || invalidReferences.length == 0) {
053                invalidRefList = Collections.emptyList();
054            } else if (invalidReferences.length == 1) {
055                invalidRefList = Collections.singletonList(invalidReferences[0]);
056            } else {
057                invalidRefList = new ArrayList<Reference>();
058                for (Reference ref : invalidReferences) {
059                    invalidRefList.add(ref);
060                }
061            }
062            this.invalidReferences.put(location, invalidRefList);
063        }
064    
065        /**
066         * @param invalidReferences the map of locations to invalid references
067         */
068        public ReferentialIntegrityException( Map<Location, List<Reference>> invalidReferences ) {
069            this.invalidReferences = invalidReferences;
070        }
071    
072        /**
073         * @param invalidReferences the map of locations to invalid references
074         * @param message
075         */
076        public ReferentialIntegrityException( Map<Location, List<Reference>> invalidReferences,
077                                              String message ) {
078            super(message);
079            this.invalidReferences = invalidReferences;
080        }
081    
082        /**
083         * @param invalidReferences the map of locations to invalid references
084         * @param cause
085         */
086        public ReferentialIntegrityException( Map<Location, List<Reference>> invalidReferences,
087                                              Throwable cause ) {
088            super(cause);
089            this.invalidReferences = invalidReferences;
090        }
091    
092        /**
093         * @param invalidReferences the map of locations to invalid references
094         * @param message
095         * @param cause
096         */
097        public ReferentialIntegrityException( Map<Location, List<Reference>> invalidReferences,
098                                              String message,
099                                              Throwable cause ) {
100            super(message, cause);
101            this.invalidReferences = invalidReferences;
102        }
103    
104        /**
105         * {@inheritDoc}
106         */
107        @Override
108        public String toString() {
109            return super.toString();
110        }
111    
112        /**
113         * @return invalidReferences
114         */
115        public Map<Location, List<Reference>> getInvalidReferences() {
116            return invalidReferences;
117        }
118    }