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