| 1 |
/* |
| 2 |
* Copyright (C) 2007 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 |
package io.airlift.slice; |
| 17 |
|
| 18 |
import javax.annotation.Nullable; |
| 19 |
|
| 20 |
// forked from com.google.common.base.Preconditions |
| 21 |
final class Preconditions |
| 22 |
{ |
| 23 |
private Preconditions() |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
public static <T> T checkNotNull(T reference, String errorMessage) |
| 28 |
{ |
| 29 |
if (reference == null) { |
| 30 |
throw new NullPointerException(errorMessage); |
| 31 |
} |
| 32 |
return reference; |
| 33 |
} |
| 34 |
|
| 35 |
public static void checkArgument(boolean expression, String errorMessage) |
| 36 |
{ |
| 37 |
if (!expression) { |
| 38 |
throw new IllegalArgumentException(errorMessage); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public static void checkArgument(boolean expression, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) |
| 43 |
{ |
| 44 |
if (!expression) { |
| 45 |
throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs)); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public static int checkPositionIndex(int index, int size) |
| 50 |
{ |
| 51 |
return checkPositionIndex(index, size, "index"); |
| 52 |
} |
| 53 |
|
| 54 |
public static int checkPositionIndex(int index, int size, @Nullable String desc) |
| 55 |
{ |
| 56 |
// Carefully optimized for execution by hotspot (explanatory comment above) |
| 57 |
if (index < 0 || index > size) { |
| 58 |
throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc)); |
| 59 |
} |
| 60 |
return index; |
| 61 |
} |
| 62 |
|
| 63 |
public static long checkPositionIndex(long index, long size) |
| 64 |
{ |
| 65 |
return checkPositionIndex(index, size, "index"); |
| 66 |
} |
| 67 |
|
| 68 |
public static long checkPositionIndex(long index, long size, @Nullable String desc) |
| 69 |
{ |
| 70 |
// Carefully optimized for execution by hotspot (explanatory comment above) |
| 71 |
if (index < 0 || index > size) { |
| 72 |
throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc)); |
| 73 |
} |
| 74 |
return index; |
| 75 |
} |
| 76 |
|
| 77 |
private static String badPositionIndex(long index, long size, String desc) |
| 78 |
{ |
| 79 |
if (index < 0) { |
| 80 |
return format("%s (%s) must not be negative", desc, index); |
| 81 |
} |
| 82 |
else if (size < 0) { |
| 83 |
throw new IllegalArgumentException("negative size: " + size); |
| 84 |
} |
| 85 |
else { // index > size |
| 86 |
return format("%s (%s) must not be greater than size (%s)", desc, index, size); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public static void checkPositionIndexes(int start, int end, int size) |
| 91 |
{ |
| 92 |
// Carefully optimized for execution by hotspot (explanatory comment above) |
| 93 |
if (start < 0 || end < start || end > size) { |
| 94 |
throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size)); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private static String badPositionIndexes(int start, int end, int size) |
| 99 |
{ |
| 100 |
if (start < 0 || start > size) { |
| 101 |
return badPositionIndex(start, size, "start index"); |
| 102 |
} |
| 103 |
if (end < 0 || end > size) { |
| 104 |
return badPositionIndex(end, size, "end index"); |
| 105 |
} |
| 106 |
// end < start |
| 107 |
return format("end index (%s) must not be less than start index (%s)", end, start); |
| 108 |
} |
| 109 |
|
| 110 |
private static String format(String template, Object... args) |
| 111 |
{ |
| 112 |
template = String.valueOf(template); // null -> "null" |
| 113 |
|
| 114 |
// start substituting the arguments into the '%s' placeholders |
| 115 |
StringBuilder builder = new StringBuilder(template.length() + 16 * args.length); |
| 116 |
int templateStart = 0; |
| 117 |
int i = 0; |
| 118 |
while (i < args.length) { |
| 119 |
int placeholderStart = template.indexOf("%s", templateStart); |
| 120 |
if (placeholderStart == -1) { |
| 121 |
break; |
| 122 |
} |
| 123 |
builder.append(template.substring(templateStart, placeholderStart)); |
| 124 |
builder.append(args[i++]); |
| 125 |
templateStart = placeholderStart + 2; |
| 126 |
} |
| 127 |
builder.append(template.substring(templateStart)); |
| 128 |
|
| 129 |
// if we run out of placeholders, append the extra args in square braces |
| 130 |
if (i < args.length) { |
| 131 |
builder.append(" ["); |
| 132 |
builder.append(args[i++]); |
| 133 |
while (i < args.length) { |
| 134 |
builder.append(", "); |
| 135 |
builder.append(args[i++]); |
| 136 |
} |
| 137 |
builder.append(']'); |
| 138 |
} |
| 139 |
|
| 140 |
return builder.toString(); |
| 141 |
} |
| 142 |
} |