Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/ColumnFormat.java
41159 views
/*1* Copyright (c) 2004, 2018, 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 class to represent the format for a column of data.31*32* @author Brian Doherty33* @since 1.534*/35public class ColumnFormat extends OptionFormat {36private int number;37private int width;38private Alignment align = Alignment.CENTER;39private Scale scale = Scale.RAW;40private String format;41private String header;42private Expression expression;43private boolean required = false;44private Object previousValue;4546public ColumnFormat(int number) {47super("Column" + number);48this.number = number;49}5051/*52* method to apply various validation rules to the ColumnFormat object.53*/54public void validate() throws ParserException {5556// if we allow column spanning, then this method must change. it57// should allow null data statments5859if (expression == null) {60// current policy is that a data statement must be specified61throw new ParserException("Missing data statement in column " + number);62}63if (header == null) {64// current policy is that if a header is not specified, then we65// will use the last component of the name as the header and66// insert the default anchor characters for center alignment..67throw new ParserException("Missing header statement in column " + number);68}69if (format == null) {70// if no formating is specified, then the format is set to output71// the raw data.72format="0";73}7475// Adjust required flag76expression.setRequired(required);77}7879public void setWidth(int width) {80this.width = width;81}8283public void setAlignment(Alignment align) {84this.align = align;85}8687public void setScale(Scale scale) {88this.scale = scale;89}9091public void setFormat(String format) {92this.format = format;93}9495public void setHeader(String header) {96this.header = header;97}9899public String getHeader() {100return header;101}102103public String getFormat() {104return format;105}106107public int getWidth() {108return width;109}110111public Alignment getAlignment() {112return align;113}114115public Scale getScale() {116return scale;117}118119public Expression getExpression() {120return expression;121}122123public void setExpression(Expression e) {124this.expression = e;125}126127public void setRequired(boolean r) {128this.required = r;129}130131public boolean isRequired() {132return this.required;133}134135public void setPreviousValue(Object o) {136this.previousValue = o;137}138139public Object getPreviousValue() {140return previousValue;141}142143public void printFormat(int indentLevel) {144String indentAmount = " ";145146StringBuilder indent = new StringBuilder("");147for (int j = 0; j < indentLevel; j++) {148indent.append(indentAmount);149}150151System.out.println(indent + name + " {");152System.out.println(indent + indentAmount + "name=" + name153+ ";data=" + expression.toString() + ";header=" + header154+ ";format=" + format + ";width=" + width155+ ";scale=" + scale.toString() + ";align=" + align.toString()156+ ";required=" + required);157158for (Iterator<OptionFormat> i = children.iterator(); i.hasNext(); /* empty */) {159OptionFormat of = i.next();160of.printFormat(indentLevel+1);161}162163System.out.println(indent + "}");164}165166public String getValue() {167return null;168}169}170171172