|
| 1 | +/* |
| 2 | + * Copyright 2024 DiffPlug |
| 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 com.diffplug.spotless; |
| 17 | + |
| 18 | +import java.util.AbstractList; |
| 19 | + |
| 20 | +import javax.annotation.Nullable; |
| 21 | + |
| 22 | +/** |
| 23 | + * Fixed-size list which maintains a list of exceptions, one per step of the formatter. |
| 24 | + * Usually this list will be empty or have only a single value, so it is optimized for stack allocation in those cases. |
| 25 | + */ |
| 26 | +class ValuePerStep<T> extends AbstractList<T> { |
| 27 | + private final int size; |
| 28 | + private @Nullable T value; |
| 29 | + private int valueIdx; |
| 30 | + private @Nullable Object[] multipleValues = null; |
| 31 | + |
| 32 | + ValuePerStep(Formatter formatter) { |
| 33 | + this.size = formatter.getSteps().size(); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public @Nullable T set(int index, T exception) { |
| 38 | + if (index < 0 || index >= size) { |
| 39 | + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); |
| 40 | + } |
| 41 | + if (this.value == null) { |
| 42 | + this.valueIdx = index; |
| 43 | + this.value = exception; |
| 44 | + return null; |
| 45 | + } else if (this.multipleValues != null) { |
| 46 | + T previousValue = (T) multipleValues[index]; |
| 47 | + multipleValues[index] = exception; |
| 48 | + return previousValue; |
| 49 | + } else { |
| 50 | + if (index == valueIdx) { |
| 51 | + T previousValue = this.value; |
| 52 | + this.value = exception; |
| 53 | + return previousValue; |
| 54 | + } else { |
| 55 | + multipleValues = new Object[size]; |
| 56 | + multipleValues[valueIdx] = this.value; |
| 57 | + multipleValues[index] = exception; |
| 58 | + return null; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public T get(int index) { |
| 65 | + if (multipleValues != null) { |
| 66 | + return (T) multipleValues[index]; |
| 67 | + } else if (valueIdx == index) { |
| 68 | + return value; |
| 69 | + } else { |
| 70 | + return null; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public int indexOfFirstValue() { |
| 75 | + if (multipleValues != null) { |
| 76 | + for (int i = 0; i < multipleValues.length; i++) { |
| 77 | + if (multipleValues[i] != null) { |
| 78 | + return i; |
| 79 | + } |
| 80 | + } |
| 81 | + return -1; |
| 82 | + } else if (value != null) { |
| 83 | + return valueIdx; |
| 84 | + } else { |
| 85 | + return -1; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public int size() { |
| 91 | + return size; |
| 92 | + } |
| 93 | +} |
0 commit comments