View Javadoc

1   package org.apache.maven.continuum.notification.mail;
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 org.apache.maven.continuum.project.ContinuumProjectState;
23  
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  
27  /**
28   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
29   * @version $Id: FormatterTool.java 1372260 2012-08-13 04:29:09Z brett $
30   */
31  public class FormatterTool
32  {
33      private final String timestampFormatString;
34  
35      private final ThreadLocal<SimpleDateFormat> timestampFormat = new ThreadLocal<SimpleDateFormat>();
36  
37      public FormatterTool( String timestampFormatString )
38      {
39          this.timestampFormatString = timestampFormatString;
40      }
41  
42      // TODO: Add i18n
43      public String formatProjectState( int state )
44      {
45          if ( state == ContinuumProjectState.NEW || state == ContinuumProjectState.CHECKEDOUT )
46          {
47              return "New";
48          }
49          else if ( state == ContinuumProjectState.OK )
50          {
51              return "Ok";
52          }
53          else if ( state == ContinuumProjectState.FAILED )
54          {
55              return "Failed";
56          }
57          else if ( state == ContinuumProjectState.ERROR )
58          {
59              return "Error";
60          }
61          else if ( state == ContinuumProjectState.BUILDING )
62          {
63              return "Building";
64          }
65          else
66          {
67              return "Unknown project state '" + state + "'";
68          }
69      }
70  
71      public String formatTrigger( int trigger )
72      {
73          if ( trigger == ContinuumProjectState.TRIGGER_SCHEDULED )
74          {
75              // TODO: fix this
76              return "Schedule";
77          }
78          else if ( trigger == ContinuumProjectState.TRIGGER_FORCED )
79          {
80              return "Forced";
81          }
82          else
83          {
84              return "Unknown build trigger: '" + trigger + "'";
85          }
86      }
87  
88      public String formatTimestamp( long timestamp )
89      {
90          if ( timestamp <= 0 )
91          {
92              return null;
93          }
94          return getSimpleDateFormat( timestampFormat, timestampFormatString ).format( new Date( timestamp ) );
95      }
96  
97      public String formatInterval( long start, long end )
98      {
99          long diff = end - start;
100 
101         long interval = diff / 1000L;
102 
103         long hours = interval / 3600L;
104 
105         interval -= hours * 3600;
106 
107         long minutes = interval / 60;
108 
109         interval -= minutes * 60;
110 
111         long seconds = interval;
112 
113         if ( hours > 0 )
114         {
115             return Long.toString( hours ) + "h " + Long.toString( minutes ) + "m " + Long.toString( seconds ) + "s";
116         }
117 
118         if ( minutes > 0 )
119         {
120             return Long.toString( minutes ) + "m " + Long.toString( seconds ) + "s";
121         }
122 
123         return Long.toString( seconds ) + "s";
124     }
125 
126     // ----------------------------------------------------------------------
127     //
128     // ----------------------------------------------------------------------
129 
130     private SimpleDateFormat getSimpleDateFormat( ThreadLocal<SimpleDateFormat> threadLocal, String format )
131     {
132         SimpleDateFormat dateFormat = threadLocal.get();
133 
134         if ( dateFormat == null )
135         {
136             dateFormat = new SimpleDateFormat( format );
137 
138             threadLocal.set( dateFormat );
139         }
140 
141         return dateFormat;
142     }
143 
144     public String trim( String str )
145     {
146         if ( str == null )
147         {
148             return "";
149         }
150         return str.trim();
151     }
152 }