001/**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.common.util.project.model;
017
018import java.util.Properties;
019
020import org.kuali.common.util.Assert;
021import org.kuali.common.util.ObjectUtils;
022import org.kuali.common.util.PropertyUtils;
023import org.kuali.common.util.identify.Identifiable;
024
025public final class ImmutableProject implements Project, Identifiable {
026
027        private final ProjectIdentifier identifier;
028        private final String version;
029        private final Properties properties;
030
031        private final String id;
032        private final int hashCode;
033
034        public static ImmutableProject copyOf(Project project) {
035                if (project instanceof ImmutableProject) {
036                        return (ImmutableProject) project;
037                } else {
038                        return new ImmutableProject(project.getGroupId(), project.getArtifactId(), project.getVersion(), project.getProperties());
039                }
040        }
041
042        public ImmutableProject(ProjectIdentifier identifier, String version, Properties properties) {
043                // Make sure we are being configured correctly
044                Assert.noNulls(identifier, properties);
045                Assert.noBlanks(version);
046
047                // Finish setting things up
048                this.identifier = identifier;
049                this.version = version;
050                this.properties = PropertyUtils.toImmutable(properties);
051                this.id = identifier.getIdentifier() + ":" + version;
052                this.hashCode = identifier.hashCode();
053        }
054
055        public ImmutableProject(String groupId, String artifactId, String version, Properties properties) {
056                this(new ProjectIdentifier(groupId, artifactId), version, properties);
057        }
058
059        @Override
060        public String getGroupId() {
061                return identifier.getGroupId();
062        }
063
064        @Override
065        public String getArtifactId() {
066                return identifier.getArtifactId();
067        }
068
069        @Override
070        public String getVersion() {
071                return version;
072        }
073
074        @Override
075        public Properties getProperties() {
076                return properties;
077        }
078
079        @Override
080        public String getIdentifier() {
081                return id;
082        }
083
084        @Override
085        public String toString() {
086                return id;
087        }
088
089        @Override
090        public int hashCode() {
091                return hashCode;
092        }
093
094        @Override
095        public boolean equals(Object other) {
096                return ObjectUtils.equalsByToString(this, other);
097        }
098
099}