History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/tools/jstat/Alignment.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/tools/jstat/Alignment.java+125−0
@@ -0,0 +1,125 @@
1+/*
2+ * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package sun.tools.jstat;
27+
28+import java.util.*;
29+
30+/**
31+ * A typesafe enumeration for describing data alignment semantics
32+ *
33+ * @author Brian Doherty
34+ * @since 1.5
35+ */
36+public abstract class Alignment {
37+
38+ private static int nextOrdinal = 0;
39+ private static HashMap<String, Alignment> map = new HashMap<String, Alignment>();
40+ private static final String blanks = " ";
41+ private final String name;
42+ private final int value = nextOrdinal++;
43+
44+ protected abstract String align(String s, int width);
45+
46+ /**
47+ * Alignment representing a Centered alignment
48+ */
49+ public static final Alignment CENTER = new Alignment("center") {
50+ protected String align(String s, int width) {
51+ int length = s.length();
52+ if (length >= width) {
53+ return s;
54+ }
55+
56+ int pad = width - length;
57+ int pad2 = pad / 2;
58+ int padr = pad % 2;
59+ if (pad2 == 0) {
60+ // only 0 or 1 character to pad
61+ return s + blanks.substring(0, padr);
62+ } else {
63+ // pad on both sides
64+ return blanks.substring(0, pad2) + s +
65+ blanks.substring(0, pad2 + padr);
66+ }
67+ }
68+ };
69+
70+ /**
71+ * Alignment representing a Left alignment
72+ */
73+ public static final Alignment LEFT = new Alignment("left") {
74+ protected String align(String s, int width) {
75+ int length = s.length();
76+ if (length >= width) {
77+ return s;
78+ }
79+ int pad = width - length;
80+ return s+blanks.substring(0, pad);
81+ }
82+ };
83+
84+ /**
85+ * Alignment representing a Right alignment
86+ */
87+ public static final Alignment RIGHT = new Alignment("right") {
88+ protected String align(String s, int width) {
89+ int length = s.length();
90+ if (length >= width) {
91+ return s;
92+ }
93+ int pad = width - length;
94+ return blanks.substring(0, pad) + s;
95+ }
96+ };
97+
98+ /**
99+ * Maps a string value to its corresponding Alignment object.
100+ *
101+ * @param s an string to match against Alignment objects.
102+ * @return The Alignment object matching the given string.
103+ */
104+ public static Alignment toAlignment(String s) {
105+ return map.get(s);
106+ }
107+
108+ /**
109+ * Returns an enumeration of the keys for this enumerated type
110+ *
111+ * @return Set of Key Words for this enumeration.
112+ */
113+ public static Set keySet() {
114+ return map.keySet();
115+ }
116+
117+ public String toString() {
118+ return name;
119+ }
120+
121+ private Alignment(String name) {
122+ this.name = name;
123+ map.put(name, this);
124+ }
125+}