1 /*
   2  * Copyright (c) 2004, 2018, 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 class to represent the format for a column of data.
  32  *
  33  * @author Brian Doherty
  34  * @since 1.5
  35  */
  36 public class ColumnFormat extends OptionFormat {
  37     private int number;
  38     private int width;
  39     private Alignment align = Alignment.CENTER;
  40     private Scale scale = Scale.RAW;
  41     private String format;
  42     private String header;
  43     private Expression expression;
  44     private boolean forcibly = false;
  45     private Object previousValue;
  46 
  47     public ColumnFormat(int number) {
  48         super("Column" + number);
  49         this.number = number;
  50     }
  51 
  52     /*
  53      * method to apply various validation rules to the ColumnFormat object.
  54      */
  55     public void validate() throws ParserException {
  56 
  57         // if we allow column spanning, then this method must change. it
  58         // should allow null data statments
  59 
  60         if (expression == null) {
  61             // current policy is that a data statement must be specified
  62             throw new ParserException("Missing data statement in column " + number);
  63         }
  64         if (header == null) {
  65             // current policy is that if a header is not specified, then we
  66             // will use the last component of the name as the header and
  67             // insert the default anchor characters for center alignment..
  68             throw new ParserException("Missing header statement in column " + number);
  69         }
  70         if (format == null) {
  71             // if no formating is specified, then the format is set to output
  72             // the raw data.
  73             format="0";
  74         }
  75     }
  76 
  77     public void setWidth(int width) {
  78         this.width = width;
  79     }
  80 
  81     public void setAlignment(Alignment align) {
  82         this.align = align;
  83     }
  84 
  85     public void setScale(Scale scale) {
  86         this.scale = scale;
  87     }
  88 
  89     public void setFormat(String format) {
  90         this.format = format;
  91     }
  92 
  93     public void setHeader(String header) {
  94         this.header = header;
  95     }
  96 
  97     public String getHeader() {
  98         return header;
  99     }
 100 
 101     public String getFormat() {
 102         return format;
 103     }
 104 
 105     public int getWidth() {
 106         return width;
 107     }
 108 
 109     public Alignment getAlignment() {
 110         return align;
 111     }
 112 
 113     public Scale getScale() {
 114         return scale;
 115     }
 116 
 117     public Expression getExpression() {
 118         return expression;
 119     }
 120 
 121     public void setExpression(Expression e) {
 122         this.expression = e;
 123     }
 124 
 125     public void setForcibly(boolean f) {
 126         this.forcibly = f;
 127     }
 128 
 129     public boolean isForcibly() {
 130         return this.forcibly;
 131     }
 132 
 133     public void setPreviousValue(Object o) {
 134         this.previousValue = o;
 135     }
 136 
 137     public Object getPreviousValue() {
 138         return previousValue;
 139     }
 140 
 141     public void printFormat(int indentLevel) {
 142         String indentAmount = "  ";
 143 
 144         StringBuilder indent = new StringBuilder("");
 145         for (int j = 0; j < indentLevel; j++) {
 146             indent.append(indentAmount);
 147         }
 148 
 149         System.out.println(indent + name + " {");
 150         System.out.println(indent + indentAmount + "name=" + name
 151                 + ";data=" + expression.toString() + ";header=" + header
 152                 + ";format=" + format + ";width=" + width
 153                 + ";scale=" + scale.toString() + ";align=" + align.toString()
 154                 + ";forcibly=" + forcibly);
 155 
 156         for (Iterator<OptionFormat> i = children.iterator();  i.hasNext(); /* empty */) {
 157             OptionFormat of = i.next();
 158             of.printFormat(indentLevel+1);
 159         }
 160 
 161         System.out.println(indent + "}");
 162     }
 163 
 164     public String getValue() {
 165         return null;
 166     }
 167 }