1 /*
   2  * Copyright (c) 2014, 2015, 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 jdk.jshell;
  27 
  28 import java.util.Locale;
  29 import java.util.stream.Stream;
  30 import java.util.stream.StreamSupport;
  31 import javax.lang.model.element.Name;
  32 import static jdk.internal.jshell.remote.RemoteCodes.DOIT_METHOD_NAME;
  33 import static jdk.internal.jshell.remote.RemoteCodes.prefixPattern;
  34 
  35 /**
  36  * Assorted shared utilities.
  37  * @author Robert Field
  38  */
  39 class Util {
  40 
  41     static final String REPL_CLASS_PREFIX = "$REPL";
  42     static final String REPL_DOESNOTMATTER_CLASS_NAME = REPL_CLASS_PREFIX+"00DOESNOTMATTER";
  43 
  44     static final Locale PARSED_LOCALE = Locale.ROOT;
  45 
  46     static boolean isDoIt(Name name) {
  47         return isDoIt(name.toString());
  48     }
  49 
  50     static boolean isDoIt(String sname) {
  51         return sname.equals(DOIT_METHOD_NAME);
  52     }
  53 
  54     static String expunge(String s) {
  55         StringBuilder sb = new StringBuilder();
  56         for (String comp : prefixPattern.split(s)) {
  57             sb.append(comp);
  58         }
  59         return sb.toString();
  60     }
  61 
  62     static String asLetters(int i) {
  63         if (i == 0) {
  64             return "";
  65         }
  66 
  67         char buf[] = new char[33];
  68         int charPos = 32;
  69 
  70         i = -i;
  71         while (i <= -26) {
  72             buf[charPos--] = (char) ('A'-(i % 26));
  73             i = i / 26;
  74         }
  75         buf[charPos] = (char) ('A'-i);
  76 
  77         return new String(buf, charPos, (33 - charPos));
  78     }
  79 
  80 
  81     static String trimEnd(String s) {
  82         int last = s.length() - 1;
  83         int i = last;
  84         while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
  85             --i;
  86         }
  87         if (i != last) {
  88             return s.substring(0, i + 1);
  89         } else {
  90             return s;
  91         }
  92     }
  93 
  94     static <T> Stream<T> stream(Iterable<T> iterable) {
  95         return StreamSupport.stream(iterable.spliterator(), false);
  96     }
  97 
  98     static class Pair<T, U> {
  99         final T first;
 100         final U second;
 101 
 102         Pair(T first, U second) {
 103             this.first = first;
 104             this.second = second;
 105         }
 106     }
 107 }