1 package org.apache.maven.continuum.notification.mail;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24
25 import org.apache.maven.continuum.project.ContinuumProjectState;
26
27
28
29
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
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
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 }