~ubuntu-branches/debian/jessie/spring-build/jessie

« back to all changes in this revision

Viewing changes to org.springframework.build.aws.ant/src/main/java/org/springframework/build/aws/ant/TransferUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-08-24 22:10:27 UTC
  • Revision ID: james.westby@ubuntu.com-20100824221027-dsvue8cksrqzdmrw
Tags: upstream-2.5.2~git3be7d52
ImportĀ upstreamĀ versionĀ 2.5.2~git3be7d52

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 SpringSource
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
package org.springframework.build.aws.ant;
 
18
 
 
19
import java.text.DecimalFormat;
 
20
import java.text.NumberFormat;
 
21
 
 
22
/**
 
23
 * Base class with utility methods for all transfer type services.
 
24
 * 
 
25
 * @author Ben Hale
 
26
 */
 
27
abstract class TransferUtils {
 
28
 
 
29
        private static final float KILOBYTE = 1024;
 
30
 
 
31
        private static final float MEGABYTE = 1048576;
 
32
 
 
33
        private static final float SECOND = 1000;
 
34
 
 
35
        private static final NumberFormat formatter = new DecimalFormat("###,###.0");
 
36
        
 
37
        private TransferUtils() {
 
38
        }
 
39
 
 
40
        public static String getFormattedSize(long size) {
 
41
                StringBuilder sb = new StringBuilder();
 
42
                float megabytes = size / MEGABYTE;
 
43
                if (megabytes > 1) {
 
44
                        sb.append(formatter.format(megabytes));
 
45
                        sb.append(" MB");
 
46
                }
 
47
                else {
 
48
                        float kilobytes = size / KILOBYTE;
 
49
                        sb.append(formatter.format(kilobytes));
 
50
                        sb.append(" KB");
 
51
                }
 
52
                return sb.toString();
 
53
        }
 
54
 
 
55
        public static String getFormattedTime(long time) {
 
56
                StringBuilder sb = new StringBuilder();
 
57
                float seconds = time / SECOND;
 
58
                sb.append(formatter.format(seconds));
 
59
                sb.append(" s");
 
60
                return sb.toString();
 
61
        }
 
62
 
 
63
        public static String getFormattedSpeed(long size, long time) {
 
64
                StringBuilder sb = new StringBuilder();
 
65
                float seconds = time / SECOND;
 
66
                float megabytes = size / MEGABYTE;
 
67
                float megabytesPerSecond = megabytes / seconds;
 
68
                if (megabytesPerSecond > 1) {
 
69
                        sb.append(formatter.format(megabytesPerSecond));
 
70
                        sb.append(" MB/s");
 
71
                }
 
72
                else {
 
73
                        float kilobytes = size / KILOBYTE;
 
74
                        float kilobytesPerSecond = kilobytes / seconds;
 
75
                        sb.append(formatter.format(kilobytesPerSecond));
 
76
                        sb.append(" KB/s");
 
77
                }
 
78
                return sb.toString();
 
79
        }
 
80
 
 
81
}