View Javadoc
1   package org.apache.maven.plugins.shade.resource.properties;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Comparator;
25  import java.util.Enumeration;
26  import java.util.Iterator;
27  import java.util.LinkedHashSet;
28  import java.util.LinkedList;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Properties;
32  import java.util.Set;
33  
34  /**
35   * Internal Properties instance sorting its keys on iterations for store() usages.
36   * It ensures properties persistence is deterministic.
37   *
38   * IMPORTANT: this only overrides methods used across JVM in store() so ordering is not guaranteed for other cases.
39   */
40  public class SortedProperties extends Properties
41  {
42      @Override
43      public Set<Map.Entry<Object, Object>> entrySet()
44      {
45          final List<Map.Entry<Object, Object>> entries = new ArrayList<>( super.entrySet() );
46          Collections.sort( entries, new Comparator<Map.Entry<Object, Object>>()
47          {
48              @Override
49              public int compare( Map.Entry<Object, Object> o1, Map.Entry<Object, Object> o2 )
50              {
51                  return String.valueOf( o1.getKey() ).compareTo( String.valueOf( o2.getKey() ) );
52              }
53          } );
54          return new LinkedHashSet<>( entries );
55      }
56  
57      @Override
58      public synchronized Enumeration<Object> keys() // ensure it is sorted to be deterministic
59      {
60          final List<String> keys = new LinkedList<>();
61          for ( Object k : super.keySet() )
62          {
63              keys.add( (String) k );
64          }
65          Collections.sort( keys );
66          final Iterator<String> it = keys.iterator();
67          return new Enumeration<Object>()
68          {
69              @Override
70              public boolean hasMoreElements()
71              {
72                  return it.hasNext();
73              }
74  
75              @Override
76              public Object nextElement()
77              {
78                  return it.next();
79              }
80          };
81      }
82  }