]>
git.argeo.org Git - gpl/argeo-slc.git/blob - AbstractMetadata.java
d95eb54a6a095645e3d869e3a9fb80f5e71f0607
1 /*******************************************************************************
2 * Copyright (c) 2012 Sonatype, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
.metadata
;
14 import java
.util
.Collections
;
15 import java
.util
.HashMap
;
19 * A skeleton class for metadata.
21 public abstract class AbstractMetadata
25 private Metadata
newInstance( Map
<String
, String
> properties
, File file
)
27 return new DefaultMetadata( getGroupId(), getArtifactId(), getVersion(), getType(), getNature(), file
,
31 public Metadata
setFile( File file
)
33 File current
= getFile();
34 if ( ( current
== null ) ? file
== null : current
.equals( file
) )
38 return newInstance( getProperties(), file
);
41 public Metadata
setProperties( Map
<String
, String
> properties
)
43 Map
<String
, String
> current
= getProperties();
44 if ( current
.equals( properties
) || ( properties
== null && current
.isEmpty() ) )
48 return newInstance( copyProperties( properties
), getFile() );
51 public String
getProperty( String key
, String defaultValue
)
53 String value
= getProperties().get( key
);
54 return ( value
!= null ) ? value
: defaultValue
;
58 * Copies the specified metadata properties. This utility method should be used when creating new metadata instances
59 * with caller-supplied properties.
61 * @param properties The properties to copy, may be {@code null}.
62 * @return The copied and read-only properties, never {@code null}.
64 protected static Map
<String
, String
> copyProperties( Map
<String
, String
> properties
)
66 if ( properties
!= null && !properties
.isEmpty() )
68 return Collections
.unmodifiableMap( new HashMap
<String
, String
>( properties
) );
72 return Collections
.emptyMap();
77 public String
toString()
79 StringBuilder buffer
= new StringBuilder( 128 );
80 if ( getGroupId().length() > 0 )
82 buffer
.append( getGroupId() );
84 if ( getArtifactId().length() > 0 )
86 buffer
.append( ':' ).append( getArtifactId() );
88 if ( getVersion().length() > 0 )
90 buffer
.append( ':' ).append( getVersion() );
92 buffer
.append( '/' ).append( getType() );
93 return buffer
.toString();
97 * Compares this metadata with the specified object.
99 * @param obj The object to compare this metadata against, may be {@code null}.
100 * @return {@code true} if and only if the specified object is another {@link Metadata} with equal coordinates,
101 * type, nature, properties and file, {@code false} otherwise.
104 public boolean equals( Object obj
)
110 else if ( !( obj
instanceof Metadata
) )
115 Metadata that
= (Metadata
) obj
;
117 return getArtifactId().equals( that
.getArtifactId() ) && getGroupId().equals( that
.getGroupId() )
118 && getVersion().equals( that
.getVersion() ) && getType().equals( that
.getType() )
119 && getNature().equals( that
.getNature() ) && eq( getFile(), that
.getFile() )
120 && eq( getProperties(), that
.getProperties() );
123 private static <T
> boolean eq( T s1
, T s2
)
125 return s1
!= null ? s1
.equals( s2
) : s2
== null;
129 * Returns a hash code for this metadata.
131 * @return A hash code for the metadata.
134 public int hashCode()
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( getFile() );
146 private static int hash( Object obj
)
148 return ( obj
!= null ) ? obj
.hashCode() : 0;