View Javadoc
1   /*
2    * Copyright (C) 2012-2015 Christian Sterzl <christian.sterzl@gmail.com>
3    *
4    * This file is part of Bittwiddling.
5    *
6    * Bittwiddling is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Bittwiddling is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Bittwiddling.  If not, see <http://www.gnu.org/licenses/>.
18   */
19          
20  package com.vcollaborate.bitwise;
21  
22  import com.google.common.base.Joiner;
23  import com.google.common.base.Splitter;
24  import com.google.common.base.Strings;
25  
26  import java.util.List;
27  
28  public final class BinaryStringUtils {
29    public static final char SEPARATOR = '_';
30  
31    private BinaryStringUtils() {
32    }
33  
34    /**
35     * Returns {@code binaryString} zero-padded separated by {@value #SEPARATOR} in groups of 4.
36     */
37    public static final String prettyPrint(final String binaryString) {
38      return prettyPrint(binaryString, SEPARATOR);
39    }
40  
41    /**
42     * Returns {@code binaryString} zero-padded separated by {@code seaparator} in groups of 4.
43     */
44    public static final String prettyPrint(final String binaryString, final char separator) {
45      String paddedBinaryString = zeroPadString(binaryString);
46      List<String> splitted = Splitter.fixedLength(4).splitToList(paddedBinaryString);
47      return Joiner.on(separator).join(splitted);
48    }
49  
50    /**
51     * Reverse operation of {@link #prettyPrint(String)}.
52     */
53    public static final String fromPrettyString(final String prettyBinaryString) {
54      return fromPrettyString(prettyBinaryString, SEPARATOR);
55    }
56  
57    /**
58     * Reverse operation of {@link #prettyPrint(String, char)}.
59     */
60    public static final String fromPrettyString(final String prettyBinaryString,
61        final char separator) {
62      List<String> splitted = Splitter.on(separator).splitToList(prettyBinaryString);
63      return Joiner.on("").join(splitted);
64    }
65  
66    /**
67     * @see {@link Strings#padStart(String, int, char)}. Char is set to the value {@code 0}.
68     */
69    public static final String zeroPadString(final String binaryString, final int minLength) {
70      return Strings.padStart(binaryString, minLength, '0');
71    }
72  
73    /**
74     * Same as {@link #zeroPadString(String, int)}, but determines {@code minLength} automatically.
75     */
76    public static final String zeroPadString(final String binaryString) {
77      int minLength = (int) Math.ceil(binaryString.length() / 4.) * 4;
78      return zeroPadString(binaryString, minLength);
79    }
80  
81  }