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;
026
027/**
028 * A skeleton class for metadata.
029 */
030public abstract class AbstractMetadata
031    implements Metadata
032{
033
034    private Metadata newInstance( Map<String, String> properties, File file )
035    {
036        return new DefaultMetadata( getGroupId(), getArtifactId(), getVersion(), getType(), getNature(), file,
037                                    properties );
038    }
039
040    public Metadata setFile( File file )
041    {
042        File current = getFile();
043        if ( ( current == null ) ? file == null : current.equals( file ) )
044        {
045            return this;
046        }
047        return newInstance( getProperties(), file );
048    }
049
050    public Metadata setProperties( Map<String, String> properties )
051    {
052        Map<String, String> current = getProperties();
053        if ( current.equals( properties ) || ( properties == null && current.isEmpty() ) )
054        {
055            return this;
056        }
057        return newInstance( copyProperties( properties ), getFile() );
058    }
059
060    public String getProperty( String key, String defaultValue )
061    {
062        String value = getProperties().get( key );
063        return ( value != null ) ? value : defaultValue;
064    }
065
066    /**
067     * Copies the specified metadata properties. This utility method should be used when creating new metadata instances
068     * with caller-supplied properties.
069     * 
070     * @param properties The properties to copy, may be {@code null}.
071     * @return The copied and read-only properties, never {@code null}.
072     */
073    protected static Map<String, String> copyProperties( Map<String, String> properties )
074    {
075        if ( properties != null && !properties.isEmpty() )
076        {
077            return Collections.unmodifiableMap( new HashMap<String, String>( properties ) );
078        }
079        else
080        {
081            return Collections.emptyMap();
082        }
083    }
084
085    @Override
086    public String toString()
087    {
088        StringBuilder buffer = new StringBuilder( 128 );
089        if ( getGroupId().length() > 0 )
090        {
091            buffer.append( getGroupId() );
092        }
093        if ( getArtifactId().length() > 0 )
094        {
095            buffer.append( ':' ).append( getArtifactId() );
096        }
097        if ( getVersion().length() > 0 )
098        {
099            buffer.append( ':' ).append( getVersion() );
100        }
101        buffer.append( '/' ).append( getType() );
102        return buffer.toString();
103    }
104
105    /**
106     * Compares this metadata with the specified object.
107     * 
108     * @param obj The object to compare this metadata against, may be {@code null}.
109     * @return {@code true} if and only if the specified object is another {@link Metadata} with equal coordinates,
110     *         type, nature, properties and file, {@code false} otherwise.
111     */
112    @Override
113    public boolean equals( Object obj )
114    {
115        if ( obj == this )
116        {
117            return true;
118        }
119        else if ( !( obj instanceof Metadata ) )
120        {
121            return false;
122        }
123
124        Metadata that = (Metadata) obj;
125
126        return getArtifactId().equals( that.getArtifactId() ) && getGroupId().equals( that.getGroupId() )
127            && getVersion().equals( that.getVersion() ) && getType().equals( that.getType() )
128            && getNature().equals( that.getNature() ) && eq( getFile(), that.getFile() )
129            && eq( getProperties(), that.getProperties() );
130    }
131
132    private static <T> boolean eq( T s1, T s2 )
133    {
134        return s1 != null ? s1.equals( s2 ) : s2 == null;
135    }
136
137    /**
138     * Returns a hash code for this metadata.
139     * 
140     * @return A hash code for the metadata.
141     */
142    @Override
143    public int hashCode()
144    {
145        int hash = 17;
146        hash = hash * 31 + getGroupId().hashCode();
147        hash = hash * 31 + getArtifactId().hashCode();
148        hash = hash * 31 + getType().hashCode();
149        hash = hash * 31 + getNature().hashCode();
150        hash = hash * 31 + getVersion().hashCode();
151        hash = hash * 31 + hash( getFile() );
152        return hash;
153    }
154
155    private static int hash( Object obj )
156    {
157        return ( obj != null ) ? obj.hashCode() : 0;
158    }
159
160}