Skip to content

Commit

Permalink
Internally work with 'ImmutableList' instead of plain 'List'
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeach1 committed Jul 5, 2018
1 parent 6b729b7 commit b4ac574
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.apache.camel.component.dataprovider;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

Expand All @@ -21,19 +21,27 @@ public abstract class LazyDataProvider<T> implements IDataProvider<T> {
private final Lock readDataLock = dataLock.readLock();
private final Lock writeDataLock = dataLock.writeLock();

private List<T> data;
private ImmutableList<T> data;

/**
* Tells how to load the data.
* <p>
* Internally will be converted into a {@link ImmutableList}.
* </p>
*
* @return the loaded data. Never <code>null</code> but could be <i>empty</i>.
*/
public abstract List<T> loadData();
public abstract Iterable<T> loadData();

private void ensureDataLoaded() {
LockUtils.runWithLock(writeDataLock, () -> {
if (data == null) {
data = loadData();
Iterable<T> data = loadData();
if (data instanceof ImmutableList) {
this.data = (ImmutableList<T>) data;
} else {
this.data = ImmutableList.copyOf(data);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
* {@link IDataProvider} implementation which already specifies all the data in the constructor.
*
* @author <a href="mailto:christian.ribeaud@novartis.com">Christian Ribeaud</a>
*/
public class StaticDataProvider<T> implements IDataProvider<T> {

private final List<T> data;
// Immutable list is expected here
private final ImmutableList<T> data;

public StaticDataProvider(T... data) {
this(Arrays.asList(data));
this(ImmutableList.copyOf(data));
}

public StaticDataProvider(Collection<T> data) {
/**
* Constructor with provided {@link Iterable} <i>data</i>.
* <p>
* Internally will be converted into a {@link ImmutableList}.
* </p>
*
* @param data the {@link Iterable} data. Can NOT be <code>null</code>.
*/
public StaticDataProvider(Iterable<T> data) {
assert data != null : "Unspecified data";
if (data instanceof List == false) {
if (data instanceof ImmutableList == false) {
this.data = ImmutableList.copyOf(data);
} else {
this.data = ((List<T>) data);
this.data = (ImmutableList<T>) data;
}
}

Expand Down

0 comments on commit b4ac574

Please sign in to comment.