mockit.internal.state.MockInstances Maven / Gradle / Ivy
/*
* Copyright (c) 2006-2013 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.state;
import java.util.*;
import org.jetbrains.annotations.*;
import mockit.*;
import mockit.internal.util.*;
/**
* Holds a list of instances of mock classes (either regular classes provided by client code, or startup mock classes
* provided internally by JMockit or by external jars).
*
* This is needed to allow each redefined real method to call the corresponding mock method on the instance of the mock
* class (unless the mock method is {@code static}).
*/
public final class MockInstances
{
@NotNull private final List> mocks = new ArrayList>();
int getInstanceCount() { return mocks.size(); }
@Nullable MockUp> getMock(int index) { return mocks.get(index); }
public int addMock(@NotNull MockUp> mock)
{
int i = Utilities.indexOfReference(mocks, mock);
if (i < 0) {
i = mocks.size();
mocks.add(mock);
}
return i;
}
void removeInstance(@NotNull MockUp> mock)
{
int i = Utilities.indexOfReference(mocks, mock);
if (i >= 0) {
mocks.set(i, null);
}
}
void removeInstances(int fromIndex)
{
for (int i = mocks.size() - 1; i >= fromIndex; i--) {
mocks.remove(i);
}
}
@Nullable MockUp> findMock(@NotNull Class> mockClass)
{
int n = mocks.size();
for (int i = 0; i < n; i++) {
MockUp> mock = mocks.get(i);
if (mock != null && mock.getClass() == mockClass) {
return mock;
}
}
return null;
}
}