Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/Alignment.java
41159 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jstat;2627import java.util.*;2829/**30* A typesafe enumeration for describing data alignment semantics31*32* @author Brian Doherty33* @since 1.534*/35public abstract class Alignment {3637private static int nextOrdinal = 0;38private static HashMap<String, Alignment> map = new HashMap<String, Alignment>();39private static final String blanks = " ";40private final String name;41private final int value = nextOrdinal++;4243protected abstract String align(String s, int width);4445/**46* Alignment representing a Centered alignment47*/48public static final Alignment CENTER = new Alignment("center") {49protected String align(String s, int width) {50int length = s.length();51if (length >= width) {52return s;53}5455int pad = width - length;56int pad2 = pad / 2;57int padr = pad % 2;58if (pad2 == 0) {59// only 0 or 1 character to pad60return s + blanks.substring(0, padr);61} else {62// pad on both sides63return blanks.substring(0, pad2) + s +64blanks.substring(0, pad2 + padr);65}66}67};6869/**70* Alignment representing a Left alignment71*/72public static final Alignment LEFT = new Alignment("left") {73protected String align(String s, int width) {74int length = s.length();75if (length >= width) {76return s;77}78int pad = width - length;79return s+blanks.substring(0, pad);80}81};8283/**84* Alignment representing a Right alignment85*/86public static final Alignment RIGHT = new Alignment("right") {87protected String align(String s, int width) {88int length = s.length();89if (length >= width) {90return s;91}92int pad = width - length;93return blanks.substring(0, pad) + s;94}95};9697/**98* Maps a string value to its corresponding Alignment object.99*100* @param s an string to match against Alignment objects.101* @return The Alignment object matching the given string.102*/103public static Alignment toAlignment(String s) {104return map.get(s);105}106107/**108* Returns an enumeration of the keys for this enumerated type109*110* @return Set of Key Words for this enumeration.111*/112public static Set<String> keySet() {113return map.keySet();114}115116public String toString() {117return name;118}119120private Alignment(String name) {121this.name = name;122map.put(name, this);123}124}125126127