1 package org.apache.maven.archiva.repository.metadata;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.maven.archiva.model.ArchivaModelCloner;
23 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
24 import org.apache.maven.archiva.model.SnapshotVersion;
26 import java.util.Iterator;
27 import java.util.List;
30 * RepositoryMetadataMerge
32 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
35 public class RepositoryMetadataMerge
37 public static ArchivaRepositoryMetadata merge( final ArchivaRepositoryMetadata mainMetadata,
38 final ArchivaRepositoryMetadata sourceMetadata )
39 throws RepositoryMetadataException
41 if ( mainMetadata == null )
43 throw new RepositoryMetadataException( "Cannot merge a null main project." );
46 if ( sourceMetadata == null )
48 throw new RepositoryMetadataException( "Cannot copy to a null parent project." );
51 ArchivaRepositoryMetadata merged = new ArchivaRepositoryMetadata();
53 merged.setGroupId( merge( mainMetadata.getGroupId(), sourceMetadata.getGroupId() ) );
55 merged.setReleasedVersion( merge( mainMetadata.getReleasedVersion(), sourceMetadata.getReleasedVersion() ) );
56 merged.setSnapshotVersion( merge( mainMetadata.getSnapshotVersion(), sourceMetadata.getSnapshotVersion() ) );
57 merged.setLastUpdated( merge( mainMetadata.getLastUpdated(), sourceMetadata.getLastUpdated() ) );
58 merged.setAvailableVersions( mergeAvailableVersions( mainMetadata.getAvailableVersions(), sourceMetadata
59 .getAvailableVersions() ) );
64 private static boolean empty( String val )
71 return ( val.trim().length() <= 0 );
74 private static SnapshotVersion merge( SnapshotVersion mainSnapshotVersion, SnapshotVersion sourceSnapshotVersion )
76 if ( sourceSnapshotVersion == null )
78 return mainSnapshotVersion;
81 if ( mainSnapshotVersion == null )
83 return ArchivaModelCloner.clone( sourceSnapshotVersion );
86 SnapshotVersion merged = new SnapshotVersion();
88 merged.setTimestamp( merge( mainSnapshotVersion.getTimestamp(), sourceSnapshotVersion.getTimestamp() ) );
90 .setBuildNumber( Math.max( mainSnapshotVersion.getBuildNumber(), sourceSnapshotVersion.getBuildNumber() ) );
95 private static String merge( String main, String source )
97 if ( empty( main ) && !empty( source ) )
105 private static List mergeAvailableVersions( List mainAvailableVersions, List sourceAvailableVersions )
107 if ( sourceAvailableVersions == null )
109 return mainAvailableVersions;
112 if ( mainAvailableVersions == null )
114 return ArchivaModelCloner.cloneAvailableVersions( sourceAvailableVersions );
117 List merged = ArchivaModelCloner.cloneAvailableVersions( mainAvailableVersions );
119 Iterator it = sourceAvailableVersions.iterator();
120 while ( it.hasNext() )
122 String sourceVersion = (String) it.next();
123 if ( !merged.contains( sourceVersion ) )
125 merged.add( sourceVersion );