This project has retired. For details please refer to its Attic page.
DefaultRepositoryStatistics xref
View Javadoc
1   package org.apache.archiva.metadata.repository.stats.model;
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.text.SimpleDateFormat;
23  import java.util.Date;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.TimeZone;
27  import java.util.stream.Collectors;
28  
29  /**
30   * Default statistics implementation
31   */
32  public class DefaultRepositoryStatistics
33      implements RepositoryStatistics
34  {
35      private Date scanEndTime;
36  
37      private Date scanStartTime;
38  
39      private long totalArtifactCount;
40  
41      private long totalArtifactFileSize;
42  
43      private long totalFileCount;
44  
45      private long totalGroupCount;
46  
47      private long totalProjectCount;
48  
49      private long newFileCount;
50  
51      public static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
52  
53      private Map<String, Long> totalCountForType = new ZeroForNullHashMap<>();
54  
55      private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
56  
57      private String repositoryId;
58  
59      private Map<String, Long> customValues;
60  
61      public static final String TYPE_PREFIX = "count-type-";
62      public static final String CUSTOM_PREFIX = "count-custom-";
63  
64      @Override
65      public Date getScanEndTime( )
66      {
67          return scanEndTime;
68      }
69  
70      public void setScanEndTime( Date scanEndTime )
71      {
72          this.scanEndTime = scanEndTime;
73      }
74  
75      @Override
76      public Date getScanStartTime( )
77      {
78          return scanStartTime;
79      }
80  
81      public void setScanStartTime( Date scanStartTime )
82      {
83          this.scanStartTime = scanStartTime;
84      }
85  
86      @Override
87      public long getTotalArtifactCount( )
88      {
89          return totalArtifactCount;
90      }
91  
92      @Override
93      public void setTotalArtifactCount( long totalArtifactCount )
94      {
95          this.totalArtifactCount = totalArtifactCount;
96      }
97  
98      @Override
99      public long getTotalArtifactFileSize( )
100     {
101         return totalArtifactFileSize;
102     }
103 
104     @Override
105     public void setTotalArtifactFileSize( long totalArtifactFileSize )
106     {
107         this.totalArtifactFileSize = totalArtifactFileSize;
108     }
109 
110     @Override
111     public long getTotalFileCount( )
112     {
113         return totalFileCount;
114     }
115 
116     @Override
117     public void setTotalFileCount( long totalFileCount )
118     {
119         this.totalFileCount = totalFileCount;
120     }
121 
122     @Override
123     public long getTotalGroupCount( )
124     {
125         return totalGroupCount;
126     }
127 
128     @Override
129     public void setTotalGroupCount( long totalGroupCount )
130     {
131         this.totalGroupCount = totalGroupCount;
132     }
133 
134     @Override
135     public long getTotalProjectCount( )
136     {
137         return totalProjectCount;
138     }
139 
140     @Override
141     public void setTotalProjectCount( long totalProjectCount )
142     {
143         this.totalProjectCount = totalProjectCount;
144     }
145 
146     @Override
147     public void setNewFileCount( long newFileCount )
148     {
149         this.newFileCount = newFileCount;
150     }
151 
152     @Override
153     public long getNewFileCount( )
154     {
155         return newFileCount;
156     }
157 
158     @Override
159     public long getDuration( )
160     {
161         return scanEndTime.getTime() - scanStartTime.getTime();
162     }
163 
164     @Override
165     public String getRepositoryId( )
166     {
167         return repositoryId;
168     }
169 
170     public void setRepositoryId( String repositoryId )
171     {
172         this.repositoryId = repositoryId;
173     }
174 
175     @Override
176     public String getFacetId()
177     {
178         return FACET_ID;
179     }
180 
181     @Override
182     public String getName()
183     {
184         return createNameFormat().format( scanStartTime );
185     }
186 
187     private static SimpleDateFormat createNameFormat()
188     {
189         SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
190         fmt.setTimeZone( UTC_TIME_ZONE );
191         return fmt;
192     }
193 
194     @Override
195     public Map<String, String> toProperties()
196     {
197         Map<String, String> properties = new HashMap<>();
198         if (scanEndTime==null) {
199             properties.put("scanEndTime", "0");
200         } else
201         {
202             properties.put( "scanEndTime", String.valueOf( scanEndTime.getTime( ) ) );
203         }
204         if (scanStartTime==null) {
205             properties.put("scanStartTime","0");
206         } else
207         {
208             properties.put( "scanStartTime", String.valueOf( scanStartTime.getTime( ) ) );
209         }
210         properties.put( "totalArtifactCount", String.valueOf( totalArtifactCount ) );
211         properties.put( "totalArtifactFileSize", String.valueOf( totalArtifactFileSize ) );
212         properties.put( "totalFileCount", String.valueOf( totalFileCount ) );
213         properties.put( "totalGroupCount", String.valueOf( totalGroupCount ) );
214         properties.put( "totalProjectCount", String.valueOf( totalProjectCount ) );
215         properties.put( "newFileCount", String.valueOf( newFileCount ) );
216         properties.put( "repositoryId", repositoryId );
217         for ( Map.Entry<String, Long> entry : totalCountForType.entrySet() )
218         {
219             properties.put( TYPE_PREFIX + entry.getKey(), String.valueOf( entry.getValue() ) );
220         }
221         if (customValues!=null) {
222             for (Map.Entry<String, Long> entry : customValues.entrySet()) {
223                 properties.put(CUSTOM_PREFIX+entry.getKey(), String.valueOf(entry.getValue()));
224             }
225         }
226         return properties;
227     }
228 
229     @Override
230     public void fromProperties( Map<String, String> properties )
231     {
232         scanEndTime = new Date( Long.parseLong( properties.get( "scanEndTime" ) ) );
233         scanStartTime = new Date( Long.parseLong( properties.get( "scanStartTime" ) ) );
234         totalArtifactCount = Long.parseLong( properties.get( "totalArtifactCount" ) );
235         totalArtifactFileSize = Long.parseLong( properties.get( "totalArtifactFileSize" ) );
236         totalFileCount = Long.parseLong( properties.get( "totalFileCount" ) );
237         totalGroupCount = Long.parseLong( properties.get( "totalGroupCount" ) );
238         totalProjectCount = Long.parseLong( properties.get( "totalProjectCount" ) );
239         newFileCount = Long.parseLong( properties.get( "newFileCount" ) );
240         repositoryId = properties.get( "repositoryId" );
241         totalCountForType.clear();
242         for ( Map.Entry<String, String> entry : properties.entrySet() )
243         {
244             if ( entry.getKey().startsWith( TYPE_PREFIX ) )
245             {
246                 totalCountForType.put( entry.getKey().substring( TYPE_PREFIX.length() ), Long.valueOf( entry.getValue() ) );
247             } else if (entry.getKey().startsWith( CUSTOM_PREFIX )) {
248                 if (customValues==null) {
249                     createCustomValueMap();
250                 }
251                 customValues.put(entry.getKey().substring( CUSTOM_PREFIX.length() ), Long.valueOf(entry.getValue()));
252             }
253         }
254     }
255 
256 
257     @Override
258     public boolean equals( Object o )
259     {
260         if ( this == o )
261         {
262             return true;
263         }
264         if ( o == null || getClass() != o.getClass() )
265         {
266             return false;
267         }
268 
269         DefaultRepositoryStatistics./org/apache/archiva/metadata/repository/stats/model/DefaultRepositoryStatistics.html#DefaultRepositoryStatistics">DefaultRepositoryStatistics that = (DefaultRepositoryStatistics) o;
270 
271         if ( newFileCount != that.newFileCount )
272         {
273             return false;
274         }
275         if ( totalArtifactCount != that.totalArtifactCount )
276         {
277             return false;
278         }
279         if ( totalArtifactFileSize != that.totalArtifactFileSize )
280         {
281             return false;
282         }
283         if ( totalFileCount != that.totalFileCount )
284         {
285             return false;
286         }
287         if ( totalGroupCount != that.totalGroupCount )
288         {
289             return false;
290         }
291         if ( totalProjectCount != that.totalProjectCount )
292         {
293             return false;
294         }
295         if ( !scanEndTime.equals( that.scanEndTime ) )
296         {
297             return false;
298         }
299         if ( !scanStartTime.equals( that.scanStartTime ) )
300         {
301             return false;
302         }
303         if ( !totalCountForType.equals( that.totalCountForType ) )
304         {
305             return false;
306         }
307         if ( customValues==null && that.customValues!=null) {
308             return false;
309         }
310         if ( customValues!=null && that.customValues==null) {
311             return false;
312         }
313         if (customValues!=null && !customValues.equals(that.customValues)) {
314             return false;
315         }
316         return repositoryId.equals( that.repositoryId );
317     }
318 
319     @Override
320     public int hashCode()
321     {
322         int result = scanEndTime.hashCode();
323         result = 31 * result + scanStartTime.hashCode();
324         result = 31 * result + (int) ( totalArtifactCount ^ ( totalArtifactCount >>> 32 ) );
325         result = 31 * result + (int) ( totalArtifactFileSize ^ ( totalArtifactFileSize >>> 32 ) );
326         result = 31 * result + (int) ( totalFileCount ^ ( totalFileCount >>> 32 ) );
327         result = 31 * result + (int) ( totalGroupCount ^ ( totalGroupCount >>> 32 ) );
328         result = 31 * result + (int) ( totalProjectCount ^ ( totalProjectCount >>> 32 ) );
329         result = 31 * result + (int) ( newFileCount ^ ( newFileCount >>> 32 ) );
330         result = 31 * result + totalCountForType.hashCode();
331         result = 31 * result + repositoryId.hashCode();
332         if (customValues!=null)
333             result = 31 * result + customValues.hashCode();
334         return result;
335     }
336 
337     @Override
338     public String toString()
339     {
340         return "RepositoryStatistics{" + "scanEndTime=" + scanEndTime + ", scanStartTime=" + scanStartTime +
341             ", totalArtifactCount=" + totalArtifactCount + ", totalArtifactFileSize=" + totalArtifactFileSize +
342             ", totalFileCount=" + totalFileCount + ", totalGroupCount=" + totalGroupCount + ", totalProjectCount=" +
343             totalProjectCount + ", newFileCount=" + newFileCount + ", totalCountForType=" + totalCountForType + ", " +
344             "repositoryId=" + repositoryId +
345             getCustomValueString() +
346             '}';
347     }
348 
349     private String getCustomValueString() {
350         if (customValues==null) {
351             return "";
352         } else {
353             return customValues.entrySet().stream().map(entry -> entry.getKey()+"="+entry.getValue()).collect(
354                 Collectors.joining( ",")
355             );
356         }
357     }
358 
359     @Override
360     public Map<String, Long> getTotalCountForType( )
361     {
362         return totalCountForType;
363     }
364 
365     @Override
366     public long getTotalCountForType( String type )
367     {
368         return totalCountForType.get( type );
369     }
370 
371     @Override
372     public void setTotalCountForType( String type, long count )
373     {
374         totalCountForType.put( type, count );
375     }
376 
377     @Override
378     public long getCustomValue( String fieldName )
379     {
380         // Lazy evaluation, because it may not be used very often.
381         if (customValues==null) {
382             createCustomValueMap();
383         }
384         return customValues.get(fieldName);
385     }
386 
387     @Override
388     public void setCustomValue( String fieldName, long count )
389     {
390         // Lazy evaluation, because it may not be used very often.
391         if (customValues==null) {
392             createCustomValueMap();
393         }
394         customValues.put(fieldName, count);
395     }
396 
397     private void createCustomValueMap( )
398     {
399         customValues = new ZeroForNullHashMap<>();
400     }
401 
402 
403     private static final class ZeroForNullHashMap<K> extends HashMap<K, Long>
404     {   
405         @Override
406         public Long get(Object key) {
407             Long value = super.get( key );
408             
409             return ( value != null ) ? value : Long.valueOf( 0L );
410         }
411     }
412 }