1/*
2 * Copyright (C) 2008 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect.testing;
18
19/**
20 * Minimal GWT emulation of {@code com.google.common.collect.testing.Platform}.
21 *
22 * <p><strong>This .java file should never be consumed by javac.</strong>
23 *
24 * @author Hayward Chan
25 */
26final class Platform {
27
28  static boolean checkIsInstance(Class<?> clazz, Object obj) {
29    /*
30     * In GWT, we can't tell whether obj is an instance of clazz because GWT
31     * doesn't support reflections.  For testing purposes, we give up this
32     * particular assertion (so that we can keep the rest).
33     */
34    return true;
35  }
36
37  // Class.cast is not supported in GWT.
38  static void checkCast(Class<?> clazz, Object obj) {
39  }
40
41  static <T> T[] clone(T[] array) {
42    return GwtPlatform.clone(array);
43  }
44
45  // TODO: Consolidate different copies in one single place.
46  static String format(String template, Object... args) {
47    // start substituting the arguments into the '%s' placeholders
48    StringBuilder builder = new StringBuilder(
49        template.length() + 16 * args.length);
50    int templateStart = 0;
51    int i = 0;
52    while (i < args.length) {
53      int placeholderStart = template.indexOf("%s", templateStart);
54      if (placeholderStart == -1) {
55        break;
56      }
57      builder.append(template.substring(templateStart, placeholderStart));
58      builder.append(args[i++]);
59      templateStart = placeholderStart + 2;
60    }
61    builder.append(template.substring(templateStart));
62
63    // if we run out of placeholders, append the extra args in square braces
64    if (i < args.length) {
65      builder.append(" [");
66      builder.append(args[i++]);
67      while (i < args.length) {
68        builder.append(", ");
69        builder.append(args[i++]);
70      }
71      builder.append("]");
72    }
73
74    return builder.toString();
75  }
76
77  private Platform() {}
78}
79