001/** 002 * Copyright 2010-2014 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 static org.kuali.common.util.ObjectUtils.equalByToString; 019import static org.kuali.common.util.base.Precondition.checkNotBlank; 020import static org.kuali.common.util.base.Precondition.checkNotNull; 021 022import java.util.Properties; 023 024import org.kuali.common.util.property.ImmutableProperties; 025 026public final class ImmutableProject implements Project { 027 028 private final ProjectIdentifier identifier; 029 private final String version; 030 private final ImmutableProperties properties; 031 032 private final String id; 033 private final int hashCode; 034 035 public static ImmutableProject copyOf(Project project) { 036 if (project instanceof ImmutableProject) { 037 return (ImmutableProject) project; 038 } else { 039 return new ImmutableProject(project.getGroupId(), project.getArtifactId(), project.getVersion(), project.getProperties()); 040 } 041 } 042 043 public ImmutableProject(ProjectIdentifier identifier, String version, Properties properties) { 044 checkNotNull(identifier, "identifier"); 045 checkNotBlank(version, "version"); 046 checkNotNull(properties, "properties"); 047 048 // Finish setting things up 049 this.identifier = identifier; 050 this.version = version; 051 this.properties = ImmutableProperties.copyOf(properties); 052 this.id = identifier.getIdentifier() + ":" + version; 053 this.hashCode = identifier.hashCode(); 054 } 055 056 public ImmutableProject(String groupId, String artifactId, String version, Properties properties) { 057 this(new ProjectIdentifier(groupId, artifactId), version, properties); 058 } 059 060 @Override 061 public String getGroupId() { 062 return identifier.getGroupId(); 063 } 064 065 @Override 066 public String getArtifactId() { 067 return identifier.getArtifactId(); 068 } 069 070 @Override 071 public String getVersion() { 072 return version; 073 } 074 075 @Override 076 public Properties getProperties() { 077 return properties; 078 } 079 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 equalByToString(this, other); 097 } 098 099}