View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.client;
22  
23  import java.util.Collection;
24  
25  public class Util {
26  
27      public static String getZeroPaddedNonNegativeNumber(final long val, final int width) throws HBqlException {
28  
29          if (val < 0)
30              throw new HBqlException("Value " + val + " must be non-negative");
31  
32          final String strval = "" + val;
33          final int padsize = width - strval.length();
34          if (padsize < 0)
35              throw new HBqlException("Value " + val + " exceeds width " + width);
36  
37          final StringBuilder sbuf = new StringBuilder();
38          for (int i = 0; i < padsize; i++)
39              sbuf.append("0");
40  
41          sbuf.append(strval);
42          return sbuf.toString();
43      }
44  
45      public static byte[][] getStringsAsBytes(final Collection<String> vals) {
46  
47          final byte[][] retval;
48  
49          if (vals == null) {
50              retval = null;
51          }
52          else {
53              retval = new byte[vals.size()][];
54              int cnt = 0;
55              for (final String val : vals)
56                  retval[cnt++] = val.getBytes();
57          }
58  
59          return retval;
60      }
61  
62      public static byte[] getFixedWidthString(final char c, final int len) {
63          final StringBuilder sbuf = new StringBuilder(len);
64          for (int i = 0; i < len; i++)
65              sbuf.append(c);
66          return sbuf.toString().getBytes();
67      }
68  }