001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.metadata; 020 021import java.io.File; 022import java.nio.file.Path; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.Objects; 027 028/** 029 * A skeleton class for metadata. 030 */ 031public abstract class AbstractMetadata implements Metadata { 032 033 private Metadata newInstance(Map<String, String> properties, Path path) { 034 return new DefaultMetadata( 035 getGroupId(), getArtifactId(), getVersion(), getType(), getNature(), path, properties); 036 } 037 038 @Deprecated 039 @Override 040 public Metadata setFile(File file) { 041 File current = getFile(); 042 if (Objects.equals(current, file)) { 043 return this; 044 } 045 return newInstance(getProperties(), file != null ? file.toPath() : null); 046 } 047 048 @Override 049 public Metadata setPath(Path path) { 050 Path current = getPath(); 051 if (Objects.equals(current, path)) { 052 return this; 053 } 054 return newInstance(getProperties(), path); 055 } 056 057 @Override 058 public Metadata setProperties(Map<String, String> properties) { 059 Map<String, String> current = getProperties(); 060 if (current.equals(properties) || (properties == null && current.isEmpty())) { 061 return this; 062 } 063 return newInstance(copyProperties(properties), getPath()); 064 } 065 066 @Override 067 public String getProperty(String key, String defaultValue) { 068 String value = getProperties().get(key); 069 return (value != null) ? value : defaultValue; 070 } 071 072 /** 073 * Copies the specified metadata properties. This utility method should be used when creating new metadata instances 074 * with caller-supplied properties. 075 * 076 * @param properties The properties to copy, may be {@code null}. 077 * @return The copied and read-only properties, never {@code null}. 078 */ 079 protected static Map<String, String> copyProperties(Map<String, String> properties) { 080 if (properties != null && !properties.isEmpty()) { 081 return Collections.unmodifiableMap(new HashMap<>(properties)); 082 } else { 083 return Collections.emptyMap(); 084 } 085 } 086 087 @Override 088 public String toString() { 089 StringBuilder buffer = new StringBuilder(128); 090 if (!getGroupId().isEmpty()) { 091 buffer.append(getGroupId()); 092 } 093 if (!getArtifactId().isEmpty()) { 094 buffer.append(':').append(getArtifactId()); 095 } 096 if (!getVersion().isEmpty()) { 097 buffer.append(':').append(getVersion()); 098 } 099 buffer.append('/').append(getType()); 100 return buffer.toString(); 101 } 102 103 /** 104 * Compares this metadata with the specified object. 105 * 106 * @param obj The object to compare this metadata against, may be {@code null}. 107 * @return {@code true} if and only if the specified object is another {@link Metadata} with equal coordinates, 108 * type, nature, properties and file, {@code false} otherwise. 109 */ 110 @Override 111 public boolean equals(Object obj) { 112 if (obj == this) { 113 return true; 114 } else if (!(obj instanceof Metadata)) { 115 return false; 116 } 117 118 Metadata that = (Metadata) obj; 119 120 return Objects.equals(getArtifactId(), that.getArtifactId()) 121 && Objects.equals(getGroupId(), that.getGroupId()) 122 && Objects.equals(getVersion(), that.getVersion()) 123 && Objects.equals(getType(), that.getType()) 124 && Objects.equals(getNature(), that.getNature()) 125 && Objects.equals(getPath(), that.getPath()) 126 && Objects.equals(getProperties(), that.getProperties()); 127 } 128 129 /** 130 * Returns a hash code for this metadata. 131 * 132 * @return A hash code for the metadata. 133 */ 134 @Override 135 public int hashCode() { 136 int hash = 17; 137 hash = hash * 31 + getGroupId().hashCode(); 138 hash = hash * 31 + getArtifactId().hashCode(); 139 hash = hash * 31 + getType().hashCode(); 140 hash = hash * 31 + getNature().hashCode(); 141 hash = hash * 31 + getVersion().hashCode(); 142 hash = hash * 31 + hash(getPath()); 143 return hash; 144 } 145 146 private static int hash(Object obj) { 147 return (obj != null) ? obj.hashCode() : 0; 148 } 149}