Skip to content

Instantly share code, notes, and snippets.

@clebertsuconic
Created March 2, 2022 18:16
Show Gist options
  • Save clebertsuconic/eb3820e5f0d8b8fa6f3d5c7132c1de75 to your computer and use it in GitHub Desktop.
Save clebertsuconic/eb3820e5f0d8b8fa6f3d5c7132c1de75 to your computer and use it in GitHub Desktop.
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.utils.collections;
import java.util.Arrays;
public class ExpapandableIntArray {
volatile int [] elements;
public void addElement(int element) {
if (elements == null) {
elements = new int[1];
elements[0] = element;
} else {
if (contains(element)) {
return;
}
int [] newElements = Arrays.copyOf(elements, elements.length + 1);
newElements[elements.length] = element;
this.elements = newElements;
}
}
public final boolean contains(int el) {
final int size = elements != null ? elements.length : 0;
for (int i = 0; i < size; i++) {
if (elements[i] == el) {
return true;
}
}
return false;
}
public final int size() {
return elements == null ? 0 : elements.length;
}
public final int[] elements() {
return elements;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment