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