repo stringclasses 1k
values | file_url stringlengths 96 373 | file_path stringlengths 11 294 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 6
values | commit_sha stringclasses 1k
values | retrieved_at stringdate 2026-01-04 14:45:56 2026-01-04 18:30:23 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/mock/RepositoryMockBuilder.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/mock/RepositoryMockBuilder.java | package com.mmnaseri.utils.spring.data.dsl.mock;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadata;
import com.mmnaseri.utils.spring.data.domain.impl.key.NoOpKeyGenerator;
import com.mmnaseri.utils.spring.data.dsl.factory.RepositoryFactoryBuilder;
import com.mmnaseri.utils.spring.data.error.MockBuilderException;
import com.mmnaseri.utils.spring.data.proxy.RepositoryFactory;
import com.mmnaseri.utils.spring.data.proxy.RepositoryFactoryConfiguration;
import com.mmnaseri.utils.spring.data.proxy.impl.DefaultRepositoryFactory;
import java.util.LinkedList;
import java.util.List;
/**
* This class implements the interfaces used to define a DSL for mocking a repository.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
public class RepositoryMockBuilder implements Start, ImplementationAnd, KeyGeneration {
private final RepositoryFactory factory;
private final List<Class<?>> implementations;
private final KeyGenerator<?> keyGenerator;
public RepositoryMockBuilder() {
this(null, new LinkedList<>(), null);
}
private RepositoryMockBuilder(
RepositoryFactory factory, List<Class<?>> implementations, KeyGenerator<?> keyGenerator) {
this.factory = factory;
this.implementations = implementations;
this.keyGenerator = keyGenerator;
}
@Override
public KeyGeneration useConfiguration(RepositoryFactoryConfiguration configuration) {
return new RepositoryMockBuilder(
new DefaultRepositoryFactory(configuration), implementations, keyGenerator);
}
@Override
public KeyGeneration useFactory(RepositoryFactory factory) {
return new RepositoryMockBuilder(factory, implementations, keyGenerator);
}
@Override
public ImplementationAnd usingImplementation(Class<?> implementation) {
final LinkedList<Class<?>> implementations = new LinkedList<>(this.implementations);
implementations.add(implementation);
return new RepositoryMockBuilder(factory, implementations, keyGenerator);
}
@Override
public ImplementationAnd and(Class<?> implementation) {
return usingImplementation(implementation);
}
@Override
public <S> Implementation generateKeysUsing(KeyGenerator<S> keyGenerator) {
return new RepositoryMockBuilder(factory, implementations, keyGenerator);
}
@Override
public <S, G extends KeyGenerator<S>> Implementation generateKeysUsing(Class<G> generatorType) {
//noinspection unchecked
final G instance = (G) createKeyGenerator(generatorType);
return generateKeysUsing(instance);
}
@Override
public Implementation withoutGeneratingKeys() {
return new RepositoryMockBuilder(factory, implementations, new NoOpKeyGenerator<>());
}
private KeyGenerator<?> createKeyGenerator(Class<? extends KeyGenerator> generatorType) {
final KeyGenerator instance;
try {
instance = generatorType.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new MockBuilderException(
"Failed to instantiate key generator of type " + generatorType, e);
}
return instance;
}
@Override
public <E> E mock(Class<E> repositoryInterface) {
final RepositoryFactory repositoryFactory;
if (factory == null) {
repositoryFactory = RepositoryFactoryBuilder.defaultFactory();
} else {
repositoryFactory = this.factory;
}
if (keyGenerator == null) {
final RepositoryFactoryConfiguration configuration = repositoryFactory.getConfiguration();
final KeyGenerator<?> evaluatedKeyGenerator;
if (configuration.getDefaultKeyGenerator() != null) {
evaluatedKeyGenerator = configuration.getDefaultKeyGenerator();
} else {
final KeyGeneratorProvider generatorProvider = new KeyGeneratorProvider();
final RepositoryMetadata metadata =
configuration.getRepositoryMetadataResolver().resolve(repositoryInterface);
final Class<?> identifierType = metadata.getIdentifierType();
final Class<? extends KeyGenerator<?>> keyGeneratorType =
generatorProvider.getKeyGenerator(identifierType);
evaluatedKeyGenerator = createKeyGenerator(keyGeneratorType);
}
return generateKeysUsing(evaluatedKeyGenerator).mock(repositoryInterface);
} else {
return repositoryFactory.getInstance(
keyGenerator, repositoryInterface, implementations.toArray(new Class[0]));
}
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/mock/KeyGeneratorProvider.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/mock/KeyGeneratorProvider.java | package com.mmnaseri.utils.spring.data.dsl.mock;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
import com.mmnaseri.utils.spring.data.domain.impl.key.BsonObjectIdKeyGenerator;
import com.mmnaseri.utils.spring.data.domain.impl.key.ConfigurableSequentialIntegerKeyGenerator;
import com.mmnaseri.utils.spring.data.domain.impl.key.ConfigurableSequentialLongKeyGenerator;
import com.mmnaseri.utils.spring.data.domain.impl.key.RandomIntegerKeyGenerator;
import com.mmnaseri.utils.spring.data.domain.impl.key.RandomLongKeyGenerator;
import com.mmnaseri.utils.spring.data.domain.impl.key.SequentialIntegerKeyGenerator;
import com.mmnaseri.utils.spring.data.domain.impl.key.SequentialLongKeyGenerator;
import com.mmnaseri.utils.spring.data.domain.impl.key.UUIDKeyGenerator;
import org.springframework.core.GenericTypeResolver;
import org.springframework.util.ClassUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* This class will provide a key generator for the requested key type, based on the preset list of
* available key generators.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/12/15)
*/
@SuppressWarnings("WeakerAccess")
class KeyGeneratorProvider {
private static final String OBJECT_ID_CLASS = "org.bson.types.ObjectId";
private final Map<Class<?>, List<Class<? extends KeyGenerator>>> generators;
KeyGeneratorProvider() {
final List<Class<? extends KeyGenerator>> discoveredKeyGenerators = getKeyGeneratorTypes();
generators = new ConcurrentHashMap<>();
for (Class<? extends KeyGenerator> generatorType : discoveredKeyGenerators) {
final Class<?> keyType =
GenericTypeResolver.resolveTypeArgument(generatorType, KeyGenerator.class);
assert keyType != null;
if (!generators.containsKey(keyType)) {
generators.put(keyType, new CopyOnWriteArrayList<>());
}
generators.get(keyType).add(generatorType);
}
}
private List<Class<? extends KeyGenerator>> getKeyGeneratorTypes() {
final List<Class<? extends KeyGenerator>> classes =
new ArrayList<>(
Arrays.asList(
RandomIntegerKeyGenerator.class,
RandomLongKeyGenerator.class,
SequentialIntegerKeyGenerator.class,
SequentialLongKeyGenerator.class,
ConfigurableSequentialIntegerKeyGenerator.class,
ConfigurableSequentialLongKeyGenerator.class,
UUIDKeyGenerator.class));
if (ClassUtils.isPresent(OBJECT_ID_CLASS, ClassUtils.getDefaultClassLoader())) {
classes.add(BsonObjectIdKeyGenerator.class);
}
return classes;
}
private <S> List<Class<? extends KeyGenerator<S>>> getKeyGenerators(Class<S> keyType) {
final LinkedList<Class<? extends KeyGenerator<S>>> keyGenerators = new LinkedList<>();
if (generators.containsKey(keyType)) {
addKeyGenerators(keyGenerators, keyType);
}
for (Class<?> generatorKeyType : generators.keySet()) {
if (keyType.isAssignableFrom(generatorKeyType)) {
addKeyGenerators(keyGenerators, generatorKeyType);
}
}
return keyGenerators;
}
private <S> void addKeyGenerators(
final LinkedList<Class<? extends KeyGenerator<S>>> keyGenerators,
final Class<?> generatorKeyType) {
final List<Class<? extends KeyGenerator>> classes = generators.get(generatorKeyType);
for (Class<? extends KeyGenerator> type : classes) {
keyGenerators.add((Class<? extends KeyGenerator<S>>) type);
}
}
/**
* Provides a key generator for the specified key type. This is to automate the process of getting
* a key generator, when no alternative is provided by the user.
*
* @param keyType the type of keys for which a generator is required
* @param <S> the type of keys the generator will provide
* @return the generator or {@literal null} if none could be found to satisfy the key type
*/
public <S> Class<? extends KeyGenerator<S>> getKeyGenerator(Class<S> keyType) {
final List<Class<? extends KeyGenerator<S>>> generators = getKeyGenerators(keyType);
return generators.isEmpty() ? null : generators.get(0);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/mock/ImplementationAnd.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/mock/ImplementationAnd.java | package com.mmnaseri.utils.spring.data.dsl.mock;
/**
* Lets us specify additional implementation classes
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
public interface ImplementationAnd extends End {
/**
* Tells the builder to use this additional implementation
*
* @param implementation the implementation
* @return the rest of the configuration
*/
ImplementationAnd and(Class<?> implementation);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/mock/End.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/mock/End.java | package com.mmnaseri.utils.spring.data.dsl.mock;
/**
* Lets us create a mock of the repository we have in mind
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface End {
/**
* Tells the builder that it is now time to mock the given repository interface using the
* configuration provided thus far.
*
* @param repositoryInterface the repository interface to mock
* @param <E> the type of the repository
* @return the mocked instance
*/
<E> E mock(Class<E> repositoryInterface);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/QueryDescriptionConfigurer.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/QueryDescriptionConfigurer.java | package com.mmnaseri.utils.spring.data.dsl.factory;
/**
* This interface creates a branch in the grammar that lets you either configure the operators or
* the query description extractor.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface QueryDescriptionConfigurer extends QueryDescription, Operators {}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/Auditing.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/Auditing.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import org.springframework.data.domain.AuditorAware;
/**
* Lets us set up out-of-the-box auditing
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (4/13/16, 11:20 AM)
*/
@SuppressWarnings("WeakerAccess")
public interface Auditing extends End {
/**
* Enables auditing by using the provided auditor aware
*
* @param auditorAware the auditor aware providing auditor
* @return the rest of the configuration
*/
End enableAuditing(AuditorAware auditorAware);
/**
* Enables auditing by setting the auditor to {@link
* com.mmnaseri.utils.spring.data.dsl.factory.RepositoryFactoryBuilder.DefaultAuditorAware the
* default} value.
*
* @return the rest of the configuration
*/
End enableAuditing();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/MappingContextAnd.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/MappingContextAnd.java | package com.mmnaseri.utils.spring.data.dsl.factory;
/**
* Lets us register additional mappings
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface MappingContextAnd extends OperationHandlers {
/**
* Registers an additional mapping
*
* @param superType the super type for the interface
* @param implementation the concrete class providing mapped method implementations
* @return the rest of the configuration
*/
MappingContextAnd and(Class<?> superType, Class<?> implementation);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/MappingContext.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/MappingContext.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.proxy.TypeMappingContext;
/**
* Lets us configure the type mapping
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface MappingContext extends OperationHandlers {
/**
* Tells the builder to use the provided context
*
* @param context the context
* @return the rest of the configuration
*/
EventListener withMappings(TypeMappingContext context);
/**
* Tells the builder to register a mapping
*
* @param superType the super type for the repository interface
* @param implementation the concrete class implementing the mapped methods
* @return the rest of the configuration
*/
MappingContextAnd honoringImplementation(Class<?> superType, Class<?> implementation);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/DataStores.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/DataStores.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.store.DataStore;
import com.mmnaseri.utils.spring.data.store.DataStoreRegistry;
/**
* Lets us change the data store and the data store registry
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface DataStores extends ResultAdapters {
/**
* Tells the builder to use a different registry
*
* @param registry the registry
* @return the rest of the configuration
*/
ResultAdapters withDataStores(DataStoreRegistry registry);
/**
* Registers a new data store
*
* @param dataStore the data store
* @param <E> the entity type
* @param <K> the key type
* @return the rest of the configuration
*/
<E, K> DataStoresAnd registerDataStore(DataStore<K, E> dataStore);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/Operators.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/Operators.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.domain.Operator;
import com.mmnaseri.utils.spring.data.domain.OperatorContext;
/**
* This interface lets us define the operators that are used when extracting query descriptions
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface Operators extends DataFunctions {
/**
* Tells the builder to use the provided context instead of its own context
*
* @param context the context
* @return the rest of the configuration
*/
DataFunctions withOperators(OperatorContext context);
/**
* Registers the given operator in the context used by the builder
*
* @param operator the operator
* @return the rest of the configuration
*/
OperatorsAnd registerOperator(Operator operator);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/Start.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/Start.java | package com.mmnaseri.utils.spring.data.dsl.factory;
/**
* This interface is used as the starting point of the DSL grammar for creating a repository factory
* and later mocking objects through that repository.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface Start extends MetadataResolver {}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/FallbackKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/FallbackKeyGenerator.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
/**
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (6/14/16, 10:25 PM)
*/
public interface FallbackKeyGenerator extends EventListener {
/**
* Sets up a default key generator that would be used as a fallback if no key generation scheme is
* specified for the repository
*
* @param keyGenerator the key generator to be used
* @return the rest of the configuration
*/
EventListener withDefaultKeyGenerator(KeyGenerator<?> keyGenerator);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/OperatorsAnd.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/OperatorsAnd.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.domain.Operator;
/**
* This is a conjunction that let's us define an additional operator to register
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
public interface OperatorsAnd extends DataFunctions {
/**
* Registers this operator as well
*
* @param operator the operator
* @return the rest of the configuration
*/
OperatorsAnd and(Operator operator);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/OperationHandlers.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/OperationHandlers.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.proxy.NonDataOperationHandler;
import com.mmnaseri.utils.spring.data.proxy.impl.NonDataOperationInvocationHandler;
/**
* Lets us define the operation handlers
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (4/8/16)
*/
@SuppressWarnings("WeakerAccess")
public interface OperationHandlers extends FallbackKeyGenerator {
/**
* Sets the invocation handler used for handling non-data-related operations
*
* @param invocationHandler the invocation handler
* @return the rest of the configuration
*/
FallbackKeyGenerator withOperationHandlers(NonDataOperationInvocationHandler invocationHandler);
/**
* Registers an operation handler
*
* @param handler the handler
* @return the rest of the configuration
*/
OperationHandlersAnd withOperationHandler(NonDataOperationHandler handler);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/EventListener.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/EventListener.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.store.DataStoreEvent;
import com.mmnaseri.utils.spring.data.store.DataStoreEventListener;
import com.mmnaseri.utils.spring.data.store.DataStoreEventListenerContext;
/**
* Lets us define the various listener configurations used when data operations are taking place
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface EventListener extends Auditing {
/**
* Tells the build to use this listener context
*
* @param context the context
* @return the rest of the configuration
*/
End withListeners(DataStoreEventListenerContext context);
/**
* Tells the context to register this listener
*
* @param listener the listener
* @param <E> the (super-)type of the events the listener is going to react to
* @return the rest of the configuration
*/
<E extends DataStoreEvent> EventListenerAnd withListener(DataStoreEventListener<E> listener);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/OperationHandlersAnd.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/OperationHandlersAnd.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.proxy.NonDataOperationHandler;
/**
* Lets us define an extra operation handler
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (4/8/16)
*/
@SuppressWarnings("WeakerAccess")
public interface OperationHandlersAnd extends EventListener {
/**
* Registers an extra operation handler
*
* @param handler the handler
* @return the rest of the configuration
*/
OperationHandlersAnd and(NonDataOperationHandler handler);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/DataFunctions.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/DataFunctions.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.query.DataFunction;
import com.mmnaseri.utils.spring.data.query.DataFunctionRegistry;
/**
* This interface lets us tell the builder how the data functions should be configured
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface DataFunctions extends DataStores {
/**
* Tells the builder which data function registry it should use
*
* @param registry the registry that should be used
* @return the rest of the configuration
*/
DataStores withDataFunctions(DataFunctionRegistry registry);
/**
* Registers a function and lets you add more functions
*
* @param name the name under which this function is recognized
* @param function the function
* @param <R> the type of the result
* @return the rest of the configuration
*/
<R> DataFunctionsAnd registerFunction(String name, DataFunction<R> function);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/DataStoresAnd.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/DataStoresAnd.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.store.DataStore;
/**
* Lets us add another data store
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface DataStoresAnd extends ResultAdapters {
/**
* Adds another data store
*
* @param dataStore the data store
* @param <E> the type of the entity
* @param <K> the type of the key
* @return the rest of the configuration
*/
<E, K> DataStoresAnd and(DataStore<K, E> dataStore);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/DataFunctionsAnd.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/DataFunctionsAnd.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.query.DataFunction;
/**
* This interface lets you add additional functions
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface DataFunctionsAnd extends DataStores {
/**
* adds an additional function
*
* @param name the name of the function
* @param function the function
* @param <R> the type of the result
* @return the rest of the configuration
*/
<R> DataFunctionsAnd and(String name, DataFunction<R> function);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/ResultAdapters.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/ResultAdapters.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.proxy.ResultAdapter;
import com.mmnaseri.utils.spring.data.proxy.ResultAdapterContext;
/**
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface ResultAdapters extends MappingContext {
/**
* Tells the builder to use the provided context
*
* @param context the context
* @return the rest of the configuration
*/
MappingContext withAdapters(ResultAdapterContext context);
/**
* Tells the build to register a result adapter
*
* @param adapter the adapter
* @param <E> the type of the result for the adapter
* @return the rest of the configuration
*/
<E> ResultAdaptersAnd adaptResultsUsing(ResultAdapter<E> adapter);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/MetadataResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/MetadataResolver.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadataResolver;
/**
* This interface allows us to set what resolver is used for resolving repository metadata
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface MetadataResolver extends QueryDescriptionConfigurer {
/**
* Tells the builder to use the given repository metadata resolver instead of the default it has
*
* @param metadataResolver the resolver to use
* @return the rest of the configuration
*/
QueryDescriptionConfigurer resolveMetadataUsing(RepositoryMetadataResolver metadataResolver);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/RepositoryFactoryBuilder.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/RepositoryFactoryBuilder.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
import com.mmnaseri.utils.spring.data.domain.Operator;
import com.mmnaseri.utils.spring.data.domain.OperatorContext;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadataResolver;
import com.mmnaseri.utils.spring.data.domain.impl.DefaultOperatorContext;
import com.mmnaseri.utils.spring.data.domain.impl.DefaultRepositoryMetadataResolver;
import com.mmnaseri.utils.spring.data.domain.impl.MethodQueryDescriptionExtractor;
import com.mmnaseri.utils.spring.data.dsl.mock.Implementation;
import com.mmnaseri.utils.spring.data.dsl.mock.ImplementationAnd;
import com.mmnaseri.utils.spring.data.dsl.mock.RepositoryMockBuilder;
import com.mmnaseri.utils.spring.data.proxy.NonDataOperationHandler;
import com.mmnaseri.utils.spring.data.proxy.RepositoryFactory;
import com.mmnaseri.utils.spring.data.proxy.RepositoryFactoryConfiguration;
import com.mmnaseri.utils.spring.data.proxy.ResultAdapter;
import com.mmnaseri.utils.spring.data.proxy.ResultAdapterContext;
import com.mmnaseri.utils.spring.data.proxy.TypeMappingContext;
import com.mmnaseri.utils.spring.data.proxy.impl.DefaultRepositoryFactory;
import com.mmnaseri.utils.spring.data.proxy.impl.DefaultResultAdapterContext;
import com.mmnaseri.utils.spring.data.proxy.impl.DefaultTypeMappingContext;
import com.mmnaseri.utils.spring.data.proxy.impl.ImmutableRepositoryFactoryConfiguration;
import com.mmnaseri.utils.spring.data.proxy.impl.NonDataOperationInvocationHandler;
import com.mmnaseri.utils.spring.data.query.DataFunction;
import com.mmnaseri.utils.spring.data.query.DataFunctionRegistry;
import com.mmnaseri.utils.spring.data.query.impl.DefaultDataFunctionRegistry;
import com.mmnaseri.utils.spring.data.store.DataStore;
import com.mmnaseri.utils.spring.data.store.DataStoreEvent;
import com.mmnaseri.utils.spring.data.store.DataStoreEventListener;
import com.mmnaseri.utils.spring.data.store.DataStoreEventListenerContext;
import com.mmnaseri.utils.spring.data.store.DataStoreRegistry;
import com.mmnaseri.utils.spring.data.store.impl.AuditDataEventListener;
import com.mmnaseri.utils.spring.data.store.impl.DefaultDataStoreEventListenerContext;
import com.mmnaseri.utils.spring.data.store.impl.DefaultDataStoreRegistry;
import org.springframework.data.domain.AuditorAware;
import javax.annotation.Nonnull;
import java.util.Optional;
/**
* This class implements the DSL used to configure and build a repository factory object.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public class RepositoryFactoryBuilder
implements Start,
DataFunctionsAnd,
DataStoresAnd,
EventListenerAnd,
MappingContextAnd,
OperatorsAnd,
ResultAdaptersAnd,
OperationHandlersAnd {
public static final String DEFAULT_USER = "User";
private RepositoryMetadataResolver metadataResolver;
private MethodQueryDescriptionExtractor queryDescriptionExtractor;
private DataFunctionRegistry functionRegistry;
private DataStoreRegistry dataStoreRegistry;
private ResultAdapterContext resultAdapterContext;
private TypeMappingContext typeMappingContext;
private DataStoreEventListenerContext eventListenerContext;
private NonDataOperationInvocationHandler operationInvocationHandler;
private KeyGenerator<?> defaultKeyGenerator;
private RepositoryFactoryBuilder() {
metadataResolver = new DefaultRepositoryMetadataResolver();
queryDescriptionExtractor = new MethodQueryDescriptionExtractor(new DefaultOperatorContext());
functionRegistry = new DefaultDataFunctionRegistry();
dataStoreRegistry = new DefaultDataStoreRegistry();
resultAdapterContext = new DefaultResultAdapterContext();
typeMappingContext = new DefaultTypeMappingContext();
eventListenerContext = new DefaultDataStoreEventListenerContext();
operationInvocationHandler = new NonDataOperationInvocationHandler();
// by default, we do not want any key generator, unless one is specified
defaultKeyGenerator = null;
}
@Override
public QueryDescriptionConfigurer resolveMetadataUsing(
RepositoryMetadataResolver metadataResolver) {
this.metadataResolver = metadataResolver;
return this;
}
@Override
public DataFunctions withOperators(OperatorContext context) {
queryDescriptionExtractor = new MethodQueryDescriptionExtractor(context);
return this;
}
@Override
public OperatorsAnd registerOperator(Operator operator) {
queryDescriptionExtractor.getOperatorContext().register(operator);
return this;
}
@Override
public DataFunctions extractQueriesUsing(MethodQueryDescriptionExtractor extractor) {
queryDescriptionExtractor = extractor;
return this;
}
@Override
public DataStores withDataFunctions(DataFunctionRegistry registry) {
functionRegistry = registry;
return this;
}
@Override
public <R> DataFunctionsAnd registerFunction(String name, DataFunction<R> function) {
functionRegistry.register(name, function);
return this;
}
@Override
public ResultAdapters withDataStores(DataStoreRegistry registry) {
dataStoreRegistry = registry;
return this;
}
@Override
public <E, K> DataStoresAnd registerDataStore(DataStore<K, E> dataStore) {
dataStoreRegistry.register(dataStore);
return this;
}
@Override
public MappingContext withAdapters(ResultAdapterContext context) {
resultAdapterContext = context;
return this;
}
@Override
public <E> ResultAdaptersAnd adaptResultsUsing(ResultAdapter<E> adapter) {
resultAdapterContext.register(adapter);
return this;
}
@Override
public EventListener withMappings(TypeMappingContext context) {
typeMappingContext = context;
return this;
}
@Override
public MappingContextAnd honoringImplementation(Class<?> superType, Class<?> implementation) {
typeMappingContext.register(superType, implementation);
return this;
}
@Override
public FallbackKeyGenerator withOperationHandlers(
NonDataOperationInvocationHandler invocationHandler) {
operationInvocationHandler = invocationHandler;
return this;
}
@Override
public OperationHandlersAnd withOperationHandler(NonDataOperationHandler handler) {
operationInvocationHandler.register(handler);
return this;
}
@Override
public End withListeners(DataStoreEventListenerContext context) {
eventListenerContext = context;
return this;
}
@Override
public <E extends DataStoreEvent> EventListenerAnd withListener(
DataStoreEventListener<E> listener) {
eventListenerContext.register(listener);
return this;
}
@Override
public EventListener enableAuditing(AuditorAware auditorAware) {
return (EventListener) and(new AuditDataEventListener(auditorAware));
}
@Override
public EventListener enableAuditing() {
return enableAuditing(new DefaultAuditorAware());
}
@Override
public <R> DataFunctionsAnd and(String name, DataFunction<R> function) {
functionRegistry.register(name, function);
return this;
}
@Override
public <E, K> DataStoresAnd and(DataStore<K, E> dataStore) {
dataStoreRegistry.register(dataStore);
return this;
}
@Override
public OperationHandlersAnd and(NonDataOperationHandler handler) {
operationInvocationHandler.register(handler);
return this;
}
@Override
public EventListener withDefaultKeyGenerator(KeyGenerator<?> keyGenerator) {
defaultKeyGenerator = keyGenerator;
return this;
}
@Override
public <E extends DataStoreEvent> EventListenerAnd and(DataStoreEventListener<E> listener) {
eventListenerContext.register(listener);
return this;
}
@Override
public MappingContextAnd and(Class<?> superType, Class<?> implementation) {
typeMappingContext.register(superType, implementation);
return this;
}
@Override
public OperatorsAnd and(Operator operator) {
queryDescriptionExtractor.getOperatorContext().register(operator);
return this;
}
@Override
public <E> ResultAdaptersAnd and(ResultAdapter<E> adapter) {
resultAdapterContext.register(adapter);
return this;
}
@Override
public RepositoryFactory build() {
return new DefaultRepositoryFactory(configure());
}
@Override
public RepositoryFactoryConfiguration configure() {
return new ImmutableRepositoryFactoryConfiguration(
metadataResolver,
queryDescriptionExtractor,
functionRegistry,
dataStoreRegistry,
resultAdapterContext,
typeMappingContext,
eventListenerContext,
operationInvocationHandler,
defaultKeyGenerator);
}
@Override
public <S> Implementation generateKeysUsing(KeyGenerator<S> keyGenerator) {
return new RepositoryMockBuilder().useFactory(build()).generateKeysUsing(keyGenerator);
}
@Override
public <S, G extends KeyGenerator<S>> Implementation generateKeysUsing(Class<G> generatorType) {
return new RepositoryMockBuilder().useFactory(build()).generateKeysUsing(generatorType);
}
@Override
public Implementation withoutGeneratingKeys() {
return new RepositoryMockBuilder().useFactory(build()).withoutGeneratingKeys();
}
@Override
public ImplementationAnd usingImplementation(Class<?> implementation) {
return new RepositoryMockBuilder().useFactory(build()).usingImplementation(implementation);
}
@Override
public <E> E mock(Class<E> repositoryInterface) {
return new RepositoryMockBuilder().useFactory(build()).mock(repositoryInterface);
}
/** @return the default configuration */
public static RepositoryFactoryConfiguration defaultConfiguration() {
final RepositoryFactoryBuilder builder = (RepositoryFactoryBuilder) builder();
return new ImmutableRepositoryFactoryConfiguration(
builder.metadataResolver,
builder.queryDescriptionExtractor,
builder.functionRegistry,
builder.dataStoreRegistry,
builder.resultAdapterContext,
builder.typeMappingContext,
builder.eventListenerContext,
builder.operationInvocationHandler,
builder.defaultKeyGenerator);
}
/**
* Starting point for writing code in the builder's DSL
*
* @return an instance of the builder
*/
public static Start builder() {
return new RepositoryFactoryBuilder();
}
/**
* Start the configuration DSL by considering the provided configuration as the default fallback
*
* @param configuration the fallback configuration
* @return an instance of the builder
*/
public static Start given(RepositoryFactoryConfiguration configuration) {
final RepositoryFactoryBuilder builder = new RepositoryFactoryBuilder();
builder.metadataResolver =
getOrDefault(configuration.getRepositoryMetadataResolver(), builder.metadataResolver);
builder.queryDescriptionExtractor =
getOrDefault(configuration.getDescriptionExtractor(), builder.queryDescriptionExtractor);
builder.functionRegistry =
getOrDefault(configuration.getFunctionRegistry(), builder.functionRegistry);
builder.dataStoreRegistry =
getOrDefault(configuration.getDataStoreRegistry(), builder.dataStoreRegistry);
builder.resultAdapterContext =
getOrDefault(configuration.getResultAdapterContext(), builder.resultAdapterContext);
builder.typeMappingContext =
getOrDefault(configuration.getTypeMappingContext(), builder.typeMappingContext);
builder.eventListenerContext =
getOrDefault(configuration.getEventListenerContext(), builder.eventListenerContext);
builder.operationInvocationHandler =
getOrDefault(
configuration.getOperationInvocationHandler(), builder.operationInvocationHandler);
builder.defaultKeyGenerator =
getOrDefault(configuration.getDefaultKeyGenerator(), builder.defaultKeyGenerator);
return builder;
}
private static <E> E getOrDefault(E value, E defaultValue) {
return value != null ? value : defaultValue;
}
/** @return the default factory */
public static RepositoryFactory defaultFactory() {
return new DefaultRepositoryFactory(defaultConfiguration());
}
/** An auditor aware that returns the static value of {@link #DEFAULT_USER} */
@SuppressWarnings("WeakerAccess")
public static class DefaultAuditorAware implements AuditorAware<String> {
/** @return {@link #DEFAULT_USER} */
@Override
@Nonnull
public Optional<String> getCurrentAuditor() {
return Optional.of(DEFAULT_USER);
}
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/End.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/End.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.dsl.mock.KeyGeneration;
import com.mmnaseri.utils.spring.data.proxy.RepositoryFactory;
import com.mmnaseri.utils.spring.data.proxy.RepositoryFactoryConfiguration;
/**
* Finalizes the DSL by providing a way to either choose to {@link #build() build} the factory or to
* {@link KeyGeneration continue} with the DSL and mock a repository instead, thus complementing the
* grammar for the repository factory build DSL with that of the repository mock builder.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface End extends KeyGeneration {
/**
* @return an instance of the repository factory as configured up to this point.
* @see Start for configuration options.
*/
RepositoryFactory build();
/**
* @return the {@link RepositoryFactoryConfiguration repository factory configuration} instance
* that has been created as a result of method calls via this DSL
*/
RepositoryFactoryConfiguration configure();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/ResultAdaptersAnd.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/ResultAdaptersAnd.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.proxy.ResultAdapter;
/**
* Lets us register an additional result adapter
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
public interface ResultAdaptersAnd extends MappingContext {
/**
* Registers an extra adapter
*
* @param adapter the adapter
* @param <E> the type of the result for the adapter
* @return the rest of the configuration
*/
<E> ResultAdaptersAnd and(ResultAdapter<E> adapter);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/QueryDescription.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/QueryDescription.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.domain.impl.MethodQueryDescriptionExtractor;
/**
* This interface lets us define the query description extractor
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
public interface QueryDescription extends DataFunctions {
/**
* Tells the builder to use the given query description extractor instead of the default
*
* @param extractor the extractor
* @return the rest of the configuration
*/
DataFunctions extractQueriesUsing(MethodQueryDescriptionExtractor extractor);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/EventListenerAnd.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/dsl/factory/EventListenerAnd.java | package com.mmnaseri.utils.spring.data.dsl.factory;
import com.mmnaseri.utils.spring.data.store.DataStoreEvent;
import com.mmnaseri.utils.spring.data.store.DataStoreEventListener;
/**
* Registers an extra event listener
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/14/15)
*/
@SuppressWarnings("WeakerAccess")
public interface EventListenerAnd extends Auditing {
/**
* Registers an extra event listener
*
* @param listener the listener
* @param <E> the type of the events the listener is subscribed to
* @return the rest of the configuration
*/
<E extends DataStoreEvent> EventListenerAnd and(DataStoreEventListener<E> listener);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Parameter.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Parameter.java | package com.mmnaseri.utils.spring.data.domain;
import java.util.Set;
/**
* This interface represents a "parameter" factored into matching a given entity to a preset
* criteria.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public interface Parameter {
/**
* @return the path leading to the property. This could be a nested property using the "dot
* notation" to separate
*/
String getPath();
/** @return the modifiers applying to the parameter */
Set<Modifier> getModifiers();
/**
* @return actual indices from the query method that map to the operands for this parameter. It
* should always follow that {@literal getIndices().length == getOperator().getOperands()}.
*/
int[] getIndices();
/** @return the operator for this parameter */
Operator getOperator();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Invocation.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Invocation.java | package com.mmnaseri.utils.spring.data.domain;
import java.lang.reflect.Method;
/**
* This interface encapsulates a single invocation, regardless of the object on which the method was
* invoked.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public interface Invocation {
/** @return the method that was invoked */
Method getMethod();
/** @return the arguments with which that method was invoked */
Object[] getArguments();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/KeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/KeyGenerator.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface encapsulates the process of keys being generated when we need a solid key
* generation scheme to be in place prior to entities being written to the data store.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/6/15)
*/
public interface KeyGenerator<S> {
/**
* Generates a new key and returns the value
*
* @return the generated key
*/
S generate();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Modifier.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Modifier.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This enumeration defines various modifiers that should be applied to a parameter.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public enum Modifier {
/** Indicates that the parameter's case should be ignored. */
IGNORE_CASE
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Operator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Operator.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface represents an operator used for matching entities to a preset criteria.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public interface Operator {
/** @return the name of the operator. Used for error reporting and such. */
String getName();
/**
* @return the number of operands this operator needs (besides the property on the entity). So,
* for instance, an equality check would require <strong>1</strong> operand.
*/
int getOperands();
/**
* @return the matcher taking care of checking whether or not the operator applies to the current
* entity
*/
Matcher getMatcher();
/** @return the suffix tokens used to indicate this operator in the name of a query method */
String[] getTokens();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/RepositoryMetadataResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/RepositoryMetadataResolver.java | package com.mmnaseri.utils.spring.data.domain;
/**
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
public interface RepositoryMetadataResolver {
RepositoryMetadata resolve(Class<?> repositoryInterface);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/OperatorContext.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/OperatorContext.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface is used to define a context holding definitions for operators.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public interface OperatorContext {
/**
* @param operator registers a new operator, taking care to avoid registration of new operators
* that override a predefined operator suffix.
*/
void register(Operator operator);
/**
* Finds the operator that matches the given suffix
*
* @param suffix the suffix to look for
* @return the operator that matches the suffix or {@literal null} if none matches.
*/
Operator getBySuffix(String suffix);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/KeyGeneratorAware.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/KeyGeneratorAware.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface is used to inject {@link KeyGenerator the key generator} into a concrete class
* aiming to provide method mapping for a repository.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/8/15)
*/
public interface KeyGeneratorAware<S> {
void setKeyGenerator(KeyGenerator<? extends S> keyGenerator);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Matcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/Matcher.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface encapsulates the responsibility of matching a given entity to a set of parameters
* based on the operation defined for that parameter.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public interface Matcher {
/**
* Determines if the value matches the properties or not, based on the underlying operation.
*
* @param parameter the parameter for which the match is happening.
* @param value the value being matched against.
* @param properties the properties for the invocation
* @return {@literal true} if it was a match
*/
boolean matches(Parameter parameter, Object value, Object... properties);
boolean isApplicableTo(Class<?> parameterType, Class<?>[] propertiesTypes);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/IdPropertyResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/IdPropertyResolver.java | package com.mmnaseri.utils.spring.data.domain;
/**
* An id property resolver will be capable of looking at an entity class and find the name of the
* property that is the ID property of that class based on the expected type of the identifier.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
public interface IdPropertyResolver {
/**
* Resolves the name of the ID <em>property</em>. If the property is accessible through a getter,
* it will still return the name of the underlying property accessible by the getter by converting
* the name of the getter to the bare property name.
*
* @param entityType the type of the entity on which the key is defined
* @param idType the expected type (or supertype) for the ID property
* @return the name of the property that represents the key to the entity
*/
String resolve(Class<?> entityType, Class<?> idType);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/RepositoryMetadata.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/RepositoryMetadata.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface encapsulates metadata required from a repository for the rest of this framework to
* function.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/19/15)
*/
public interface RepositoryMetadata {
/**
* @return the name of the property that will yield the key to the entities represented by this
* repository
*/
String getIdentifierProperty();
/** @return the type of the key this repository uses */
Class<?> getIdentifierType();
/** @return the type of the entities this repository represents */
Class<?> getEntityType();
/** @return the interface for the repository where actual methods will be defined */
Class<?> getRepositoryInterface();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/MatchedOperator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/MatchedOperator.java | package com.mmnaseri.utils.spring.data.domain;
/**
* Represents an operator that was picked because of a parse operation
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (4/17/16, 12:36 PM)
*/
public interface MatchedOperator extends Operator {
/** @return the suffix that was matched when looking up this operator */
String getMatchedToken();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/RepositoryMetadataAware.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/RepositoryMetadataAware.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface is used to inject {@link RepositoryMetadata the repository metadata} into a
* concrete class aiming to provide method mapping for a repository.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public interface RepositoryMetadataAware {
void setRepositoryMetadata(RepositoryMetadata repositoryMetadata);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/RepositoryAware.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/RepositoryAware.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface is used to inject the repository itself into a concrete class aiming to provide
* method mapping for a repository.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/9/15)
*/
public interface RepositoryAware<R> {
void setRepository(R repository);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/InvocationMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/InvocationMatcher.java | package com.mmnaseri.utils.spring.data.domain;
/**
* This interface is defined to let us match an object to a set of predefined criteria based on the
* values passed through a method invocation. This is used to determine whether or not an entity in
* the data store matches the parameters passed to a query method according to the criteria defined
* by that query method.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public interface InvocationMatcher {
/**
* Determines whether or not the entity matches the query
*
* @param entity the entity
* @param invocation the query method invocation
* @return {@literal true} if it was a match
*/
boolean matches(Object entity, Invocation invocation);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/DataStoreAware.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/DataStoreAware.java | package com.mmnaseri.utils.spring.data.domain;
import com.mmnaseri.utils.spring.data.store.DataStore;
/**
* This interface is used to inject {@link DataStore the data store} into a concrete class aiming to
* provide method mapping for a repository.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public interface DataStoreAware<E, K> {
<J extends K, F extends E> void setDataStore(DataStore<J, F> dataStore);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/DescribedDataStoreOperation.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/DescribedDataStoreOperation.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.Invocation;
import com.mmnaseri.utils.spring.data.proxy.RepositoryConfiguration;
import com.mmnaseri.utils.spring.data.query.DataFunction;
import com.mmnaseri.utils.spring.data.query.DataFunctionRegistry;
import com.mmnaseri.utils.spring.data.query.QueryDescriptor;
import com.mmnaseri.utils.spring.data.store.DataStore;
import com.mmnaseri.utils.spring.data.store.DataStoreOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.List;
/**
* This is a data store operation that has a description attached to it. This means that it is a
* select operation that is capable of taking care of applying a function to a given selection of
* objects.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class DescribedDataStoreOperation<K, E> implements DataStoreOperation<Object, K, E> {
private static final Log log = LogFactory.getLog(DescribedDataStoreOperation.class);
private final SelectDataStoreOperation<K, E> selectOperation;
private final DataFunctionRegistry functionRegistry;
public DescribedDataStoreOperation(
SelectDataStoreOperation<K, E> selectOperation, DataFunctionRegistry functionRegistry) {
this.selectOperation = selectOperation;
this.functionRegistry = functionRegistry;
}
@Override
public Object execute(
DataStore<K, E> store, RepositoryConfiguration configuration, Invocation invocation) {
log.info("Trying to select the data from the data store");
final List<E> selection = selectOperation.execute(store, configuration, invocation);
final QueryDescriptor descriptor = selectOperation.getDescriptor();
if (descriptor.getFunction() == null) {
log.info("No function was specified for the current selection");
return selection;
}
log.info("Executing function " + descriptor.getFunction() + " on the selected items");
final DataFunction<?> function = functionRegistry.getFunction(descriptor.getFunction());
return function.apply(store, descriptor, configuration, selection);
}
@Override
public String toString() {
return selectOperation.toString();
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/MethodInvocationDataStoreOperation.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/MethodInvocationDataStoreOperation.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.Invocation;
import com.mmnaseri.utils.spring.data.error.DataOperationExecutionException;
import com.mmnaseri.utils.spring.data.proxy.RepositoryConfiguration;
import com.mmnaseri.utils.spring.data.store.DataStore;
import com.mmnaseri.utils.spring.data.store.DataStoreOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* This is a data store operation that delivers the operation by calling to a delegate method. This
* means that the results of the operation are the same as what was returned by the method itself.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class MethodInvocationDataStoreOperation<K, E> implements DataStoreOperation<Object, K, E> {
private static final Log log = LogFactory.getLog(MethodInvocationDataStoreOperation.class);
private final Object instance;
private final Method method;
public MethodInvocationDataStoreOperation(Object instance, Method method) {
this.instance = instance;
this.method = method;
}
@Override
public Object execute(
DataStore<K, E> store, RepositoryConfiguration configuration, Invocation invocation) {
final Object result;
try {
log.info("Invoking method " + method + " to handle invocation " + invocation);
result = method.invoke(instance, invocation.getArguments());
} catch (IllegalAccessException e) {
throw new DataOperationExecutionException("Failed to access target method: " + method, e);
} catch (InvocationTargetException e) {
throw new DataOperationExecutionException(
"Method call resulted in internal error: " + method, e.getTargetException());
}
return result;
}
public Object getInstance() {
return instance;
}
public Method getMethod() {
return method;
}
@Override
public String toString() {
return method + " on " + instance;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/QueryModifiers.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/QueryModifiers.java | package com.mmnaseri.utils.spring.data.domain.impl;
/**
* This class contains a query's modifiers: the limit put on the number of results, and whether or
* not the values should be distinct.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (4/13/16, 9:25 AM)
*/
@SuppressWarnings("WeakerAccess")
class QueryModifiers {
private final int limit;
private final boolean distinct;
QueryModifiers(int limit, boolean distinct) {
this.limit = limit;
this.distinct = distinct;
}
public int getLimit() {
return limit;
}
public boolean isDistinct() {
return distinct;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/AnnotationRepositoryMetadataResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/AnnotationRepositoryMetadataResolver.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadata;
import com.mmnaseri.utils.spring.data.error.RepositoryDefinitionException;
import org.springframework.data.repository.RepositoryDefinition;
/**
* This class will try to resolve metadata from a repository interface that has been annotated with
* Spring's {@link RepositoryDefinition @RepositoryDefinition}. If the annotation is not found, it
* will throw a {@link RepositoryDefinitionException RepositoryDefinitionException}.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
public class AnnotationRepositoryMetadataResolver extends AbstractRepositoryMetadataResolver {
@Override
protected RepositoryMetadata resolveFromInterface(Class<?> repositoryInterface) {
final RepositoryDefinition definition =
repositoryInterface.getAnnotation(RepositoryDefinition.class);
if (definition == null) {
throw new RepositoryDefinitionException(
repositoryInterface,
"Expected the repository to be annotated with " + "@RepositoryDefinition");
}
final Class<?> entityType = definition.domainClass();
final Class<?> idType = definition.idClass();
String idProperty = resolveIdProperty(entityType, idType);
return new ImmutableRepositoryMetadata(idType, entityType, repositoryInterface, idProperty);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableRepositoryMetadata.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableRepositoryMetadata.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadata;
/**
* This is an immutable repository metadata.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/19/15)
*/
public class ImmutableRepositoryMetadata implements RepositoryMetadata {
private final Class<?> identifierType;
private final Class<?> entityType;
private final Class<?> repositoryInterface;
private final String identifier;
public ImmutableRepositoryMetadata(
Class<?> identifierType,
Class<?> entityType,
Class<?> repositoryInterface,
String identifier) {
this.identifierType = identifierType;
this.entityType = entityType;
this.repositoryInterface = repositoryInterface;
this.identifier = identifier;
}
@Override
public String getIdentifierProperty() {
return identifier;
}
@Override
public Class<?> getIdentifierType() {
return identifierType;
}
@Override
public Class<?> getEntityType() {
return entityType;
}
@Override
public Class<?> getRepositoryInterface() {
return repositoryInterface;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/PropertyComparator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/PropertyComparator.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.error.InvalidArgumentException;
import com.mmnaseri.utils.spring.data.query.NullHandling;
import com.mmnaseri.utils.spring.data.query.Order;
import com.mmnaseri.utils.spring.data.query.Sort;
import com.mmnaseri.utils.spring.data.query.SortDirection;
import com.mmnaseri.utils.spring.data.tools.PropertyUtils;
import java.util.Comparator;
import java.util.List;
/**
* This is a comparator that will compare two objects based on a common property. The property
* should be defined as an expression such as "x.y.z".
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public class PropertyComparator implements Comparator<Object> {
private static final NullHandling DEFAULT_NULL_HANDLING = NullHandling.NULLS_LAST;
private final NullHandling nullHandling;
private final String property;
private final SortDirection direction;
PropertyComparator(Order order) {
this.nullHandling =
order.getNullHandling() == null || NullHandling.DEFAULT.equals(order.getNullHandling())
? DEFAULT_NULL_HANDLING
: order.getNullHandling();
property = order.getProperty();
direction = order.getDirection();
}
@Override
public int compare(Object first, Object second) {
final Object firstValue = safeReadPropertyValue(first);
final Object secondValue = safeReadPropertyValue(second);
int comparison;
if (firstValue == null || secondValue == null) {
comparison = compareIfEitherIsNull(firstValue, secondValue);
} else {
comparison = compareIfCompatible(firstValue, secondValue);
}
return comparison * (SortDirection.DESCENDING.equals(direction) ? -1 : 1);
}
/**
* Returns the value of the specified {@link #property property} from the object, given that it
* exists. Otherwise, it throws an {@link InvalidArgumentException}.
*
* @param object the object to read the property from
* @return the value of the property
* @throws InvalidArgumentException If the value of the property cannot be read.
*/
private Object safeReadPropertyValue(Object object) {
Object firstValue;
try {
firstValue = PropertyUtils.getPropertyValue(object, property);
} catch (Exception e) {
throw new InvalidArgumentException(
"Failed to read property value for " + property + " on " + object, e);
}
return firstValue;
}
/**
* If either of the two values is {@literal null}, this will compare them by taking {@link
* NullHandling} into account.
*
* @param first the first value
* @param second the second value
* @return comparison results as defined by {@link Comparable#compareTo(Object)}
*/
private int compareIfEitherIsNull(Object first, Object second) {
if (first == null && second != null) {
return NullHandling.NULLS_FIRST.equals(nullHandling) ? -1 : 1;
} else if (first != null) {
return NullHandling.NULLS_FIRST.equals(nullHandling) ? 1 : -1;
} else {
return 0;
}
}
/**
* This will compare the two values if their are <em>compatible</em>, meaning one of them is of a
* type that is a super type or is the same type as the other one.
*
* @param first the first item
* @param second the second item
* @return comparison results as defined by {@link Comparable#compareTo(Object)}
* @throws InvalidArgumentException If the values of the two properties are not of the same type.
*/
@SuppressWarnings("unchecked")
private int compareIfCompatible(Object first, Object second) {
checkForComparable(first, second);
if (first.getClass().isInstance(second)) {
return ((Comparable) first).compareTo(second);
} else if (second.getClass().isInstance(first)) {
return ((Comparable) second).compareTo(first) * -1;
} else {
throw new InvalidArgumentException(
"Values for were not of the same type for property: " + property);
}
}
/**
* This method checks to make sure both values are of type {@link Comparable}
*
* @param first the first value
* @param second the second value
* @throws InvalidArgumentException if they are not
*/
private void checkForComparable(Object first, Object second) {
if (!(first instanceof Comparable) || !(second instanceof Comparable)) {
throw new InvalidArgumentException(
"Expected both values to be comparable for property: " + property);
}
}
/**
* Given a collection of objects, will sort them by taking the sort property into account.
*
* @param collection the collection of items
* @param sort the sort specification
*/
public static void sort(List<?> collection, Sort sort) {
for (int i = sort.getOrders().size() - 1; i >= 0; i--) {
collection.sort(new PropertyComparator(sort.getOrders().get(i)));
}
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/AbstractRandomKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/AbstractRandomKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
import java.util.HashSet;
import java.util.Set;
/**
* This implementation will wrap the key generation process in a procedure that prevents duplicate
* keys from being generated. Since the {@link #generate() generate} method on this class is
* <em>synchronized</em> it protects multi-threading and race issues from messing up the key
* generation, so the extending classes can easily generate keys without having to worry about such
* issues.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/8/15)
*/
public abstract class AbstractRandomKeyGenerator<S> implements KeyGenerator<S> {
private final Set<S> used = new HashSet<>();
@Override
public final synchronized S generate() {
S value;
do {
value = getNext();
} while (used.contains(value));
used.add(value);
return value;
}
/** @return the next key in the sequence. This needs not be reproducible. */
protected abstract S getNext();
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/DefaultRepositoryMetadataResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/DefaultRepositoryMetadataResolver.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadata;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.repository.RepositoryDefinition;
/**
* This resolver will combine generic based and annotation based metadata resolution and deliver
* both in a single package.
*
* <p>This is the order in which the resolution will take place:
*
* <ol>
* <li>It will first try to determine the definition by looking at {@link
* AnnotationRepositoryMetadataResolver annotations}.
* <li>It will then try to resolve the metadata using {@link AssignableRepositoryMetadataResolver
* inheritance}.
* </ol>
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
public class DefaultRepositoryMetadataResolver extends AbstractRepositoryMetadataResolver {
private static final Log log = LogFactory.getLog(DefaultRepositoryMetadataResolver.class);
private final AssignableRepositoryMetadataResolver assignableRepositoryMetadataResolver =
new AssignableRepositoryMetadataResolver();
private final AnnotationRepositoryMetadataResolver annotationRepositoryMetadataResolver =
new AnnotationRepositoryMetadataResolver();
@Override
protected RepositoryMetadata resolveFromInterface(Class<?> repositoryInterface) {
if (repositoryInterface.isAnnotationPresent(RepositoryDefinition.class)) {
log.info(
"Since the repository interface was annotated with @RepositoryDefinition we will try to resolve "
+ "the metadata using the provided annotation");
return annotationRepositoryMetadataResolver.resolve(repositoryInterface);
}
log.info(
"Since no annotation was found on the repository, we will try to read the metadata from the generic "
+ "type parameters derived from the Repository interface");
return assignableRepositoryMetadataResolver.resolve(repositoryInterface);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/SelectDataStoreOperation.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/SelectDataStoreOperation.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.Invocation;
import com.mmnaseri.utils.spring.data.proxy.RepositoryConfiguration;
import com.mmnaseri.utils.spring.data.query.Page;
import com.mmnaseri.utils.spring.data.query.QueryDescriptor;
import com.mmnaseri.utils.spring.data.query.Sort;
import com.mmnaseri.utils.spring.data.store.DataStore;
import com.mmnaseri.utils.spring.data.store.DataStoreOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* This is a data store operation that will read values from the underlying data store and match
* them up against the query description's different decision branches. Once all the values are
* loaded and filtered, it will then sort them according to the sort instruction, and then paginate
* them if necessary.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public class SelectDataStoreOperation<K, E> implements DataStoreOperation<List<E>, K, E> {
private static final Log log = LogFactory.getLog(SelectDataStoreOperation.class);
private final QueryDescriptor descriptor;
public SelectDataStoreOperation(QueryDescriptor descriptor) {
this.descriptor = descriptor;
}
public QueryDescriptor getDescriptor() {
return descriptor;
}
@Override
public List<E> execute(
DataStore<K, E> store, RepositoryConfiguration configuration, Invocation invocation) {
log.info("Selecting the data according to the provided selection descriptor: " + descriptor);
final List<E> selection = new LinkedList<>();
final Collection<E> all = new LinkedList<>(store.retrieveAll());
for (E entity : all) {
if (descriptor.matches(entity, invocation)) {
selection.add(entity);
}
}
log.info("Matched " + selection.size() + " items from the data store");
if (descriptor.isDistinct()) {
final Set<E> distinctValues = new HashSet<>(selection);
selection.clear();
selection.addAll(distinctValues);
log.info("After clearing up duplicates, " + selection.size() + " items remained");
}
final Sort sort = descriptor.getSort(invocation);
final Page page = descriptor.getPage(invocation);
if (sort != null) {
log.info("Sorting the selected items according to the provided ordering");
PropertyComparator.sort(selection, sort);
}
if (page != null) {
log.info("We need to paginate the selection to fit the selection criteria");
int start = page.getPageSize() * page.getPageNumber();
int finish = Math.min(start + page.getPageSize(), selection.size());
if (start > selection.size()) {
selection.clear();
} else {
final List<E> view = new ArrayList<>(selection.subList(start, finish));
selection.clear();
selection.addAll(view);
}
}
if (descriptor.getLimit() > 0) {
log.info("Going to limit the result to " + descriptor.getLimit() + " items");
final List<E> view =
new ArrayList<>(selection.subList(0, Math.min(selection.size(), descriptor.getLimit())));
selection.clear();
selection.addAll(view);
}
return selection;
}
@Override
public String toString() {
return descriptor.toString();
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/AbstractRepositoryMetadataResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/AbstractRepositoryMetadataResolver.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.IdPropertyResolver;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadata;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadataResolver;
import com.mmnaseri.utils.spring.data.domain.impl.id.EntityIdPropertyResolver;
import com.mmnaseri.utils.spring.data.error.RepositoryDefinitionException;
import java.lang.reflect.Modifier;
/**
* This class will first check for common errors in a repository's definition before letting the
* resolution proceed any further.
*
* <p>A repository must:
*
* <ul>
* <li>Not be {@literal null}
* <li>Be an interface
* <li>Be declared as {@literal public}
* </ul>
*
* <p>Once these checks are completed, it will call {@link #resolveFromInterface(Class)} to
* determine the actual metadata.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
public abstract class AbstractRepositoryMetadataResolver implements RepositoryMetadataResolver {
private final IdPropertyResolver idPropertyResolver;
protected AbstractRepositoryMetadataResolver() {
idPropertyResolver = new EntityIdPropertyResolver();
}
@Override
public final RepositoryMetadata resolve(Class<?> repositoryInterface) {
if (repositoryInterface == null) {
throw new RepositoryDefinitionException(null, "Repository interface must not be null");
}
if (!Modifier.isInterface(repositoryInterface.getModifiers())) {
throw new RepositoryDefinitionException(
repositoryInterface,
"Cannot resolve repository metadata for a class object that isn't" + " an interface");
}
if (!Modifier.isPublic(repositoryInterface.getModifiers())) {
throw new RepositoryDefinitionException(
repositoryInterface, "Repository interface needs to be declared as public");
}
return resolveFromInterface(repositoryInterface);
}
/**
* Determines the metadata from the given repository interface, knowing that the assumptions
* declared {@link AbstractRepositoryMetadataResolver the head of this class} now hold.
*
* @param repositoryInterface the repository interface.
* @return the resolved metadata for the given repository interface.
*/
protected abstract RepositoryMetadata resolveFromInterface(Class<?> repositoryInterface);
/**
* Given the type of the entity, it will determine the ID property for that entity through calling
* to {@link EntityIdPropertyResolver}
*
* @param entityType the type of the entity
* @param idType the type of the ID property
* @return the name of the ID property
*/
@SuppressWarnings("WeakerAccess")
protected String resolveIdProperty(Class<?> entityType, Class<?> idType) {
return idPropertyResolver.resolve(entityType, idType);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/DefaultOperatorContext.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/DefaultOperatorContext.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.Operator;
import com.mmnaseri.utils.spring.data.domain.OperatorContext;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.ContainingMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.EndingWithMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsBetweenMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsEqualToMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsFalseMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsGreaterThanMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsGreaterThanOrEqualToMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsInMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsLessThanMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsLessThanOrEqualToMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsLikeMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsNotBetweenMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsNotInMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsNotLikeMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsNotMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsNotNullMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsNullMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.IsTrueMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.RegexMatcher;
import com.mmnaseri.utils.spring.data.domain.impl.matchers.StartingWithMatcher;
import com.mmnaseri.utils.spring.data.error.DuplicateOperatorException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* This class is used to store the operators used in the name of a query method. Operators are
* matched by the "suffixes" eagerly (meaning that "EqualTo" will precede over "To").
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class DefaultOperatorContext implements OperatorContext {
private static final Log log = LogFactory.getLog(DefaultOperatorContext.class);
private final Set<Operator> operators;
public DefaultOperatorContext() {
this(true);
}
public DefaultOperatorContext(boolean registerDefaults) {
operators = new CopyOnWriteArraySet<>();
if (registerDefaults) {
log.info("Registering all the default operators");
operators.add(
new ImmutableOperator("AFTER", 1, new IsGreaterThanMatcher(), "After", "IsAfter"));
operators.add(
new ImmutableOperator("BEFORE", 1, new IsLessThanMatcher(), "Before", "IsBefore"));
operators.add(
new ImmutableOperator(
"CONTAINING", 1, new ContainingMatcher(), "Containing", "IsContaining", "Contains"));
operators.add(
new ImmutableOperator("BETWEEN", 2, new IsBetweenMatcher(), "Between", "IsBetween"));
operators.add(
new ImmutableOperator(
"NOT_BETWEEN", 2, new IsNotBetweenMatcher(), "NotBetween", "IsNotBetween"));
operators.add(
new ImmutableOperator(
"ENDING_WITH", 1, new EndingWithMatcher(), "EndingWith", "IsEndingWith", "EndsWith"));
operators.add(new ImmutableOperator("FALSE", 0, new IsFalseMatcher(), "False", "IsFalse"));
operators.add(
new ImmutableOperator(
"GREATER_THAN", 1, new IsGreaterThanMatcher(), "GreaterThan", "IsGreaterThan"));
operators.add(
new ImmutableOperator(
"GREATER_THAN_EQUALS",
1,
new IsGreaterThanOrEqualToMatcher(),
"GreaterThanEqual",
"IsGreaterThanEqual"));
operators.add(new ImmutableOperator("IN", 1, new IsInMatcher(), "In", "IsIn"));
operators.add(
new ImmutableOperator(
"IS", 1, new IsEqualToMatcher(), "Is", "EqualTo", "IsEqualTo", "Equals"));
operators.add(
new ImmutableOperator("NOT_NULL", 0, new IsNotNullMatcher(), "NotNull", "IsNotNull"));
operators.add(new ImmutableOperator("NULL", 0, new IsNullMatcher(), "Null", "IsNull"));
operators.add(
new ImmutableOperator("LESS_THAN", 1, new IsLessThanMatcher(), "LessThan", "IsLessThan"));
operators.add(
new ImmutableOperator(
"LESS_THAN_EQUAL",
1,
new IsLessThanOrEqualToMatcher(),
"LessThanEqual",
"IsLessThanEqual"));
operators.add(new ImmutableOperator("LIKE", 1, new IsLikeMatcher(), "Like", "IsLike"));
operators.add(new ImmutableOperator("NEAR", 1, null, "Near", "IsNear"));
operators.add(
new ImmutableOperator(
"NOT", 1, new IsNotMatcher(), "IsNot", "Not", "IsNotEqualTo", "DoesNotEqual"));
operators.add(new ImmutableOperator("NOT_IN", 1, new IsNotInMatcher(), "NotIn", "IsNotIn"));
operators.add(
new ImmutableOperator("NOT_LIKE", 1, new IsNotLikeMatcher(), "NotLike", "IsNotLike"));
operators.add(
new ImmutableOperator(
"REGEX", 1, new RegexMatcher(), "Regex", "MatchesRegex", "Matches"));
operators.add(
new ImmutableOperator(
"STARTING_WITH",
1,
new StartingWithMatcher(),
"StartingWith",
"IsStartingWith",
"StartsWith"));
operators.add(new ImmutableOperator("TRUE", 0, new IsTrueMatcher(), "True", "IsTrue"));
}
}
@Override
public void register(Operator operator) {
log.info(
"Registering operator "
+ operator.getName()
+ " which will respond to suffixes "
+ Arrays.toString(operator.getTokens()));
for (Operator item : operators) {
for (String token : item.getTokens()) {
for (String newToken : operator.getTokens()) {
if (newToken.equals(token)) {
throw new DuplicateOperatorException(item, token);
}
}
}
}
operators.add(operator);
}
@Override
public Operator getBySuffix(String suffix) {
for (Operator operator : operators) {
for (String token : operator.getTokens()) {
if (token.equals(suffix)) {
return operator;
}
}
}
return null;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/QueryDescriptionExtractor.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/QueryDescriptionExtractor.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadata;
import com.mmnaseri.utils.spring.data.proxy.RepositoryFactoryConfiguration;
import com.mmnaseri.utils.spring.data.query.QueryDescriptor;
/**
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (6/9/16, 12:29 AM)
*/
public interface QueryDescriptionExtractor<T> {
QueryDescriptor extract(
RepositoryMetadata repositoryMetadata,
RepositoryFactoryConfiguration configuration,
T target);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableInvocation.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableInvocation.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.Invocation;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* This is an immutable invocation.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
public class ImmutableInvocation implements Invocation {
private final Method method;
private final Object[] arguments;
public ImmutableInvocation(Method method, Object[] arguments) {
this.method = method;
this.arguments = arguments;
}
@Override
public Method getMethod() {
return method;
}
@Override
public Object[] getArguments() {
return arguments;
}
@Override
public String toString() {
return method + ", " + Arrays.toString(arguments);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableParameter.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableParameter.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.Modifier;
import com.mmnaseri.utils.spring.data.domain.Operator;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* This is an immutable parameter.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public class ImmutableParameter implements Parameter {
private final String path;
private final Set<Modifier> modifiers;
private final int[] indices;
private final Operator operator;
public ImmutableParameter(
String path, Set<Modifier> modifiers, int[] indices, Operator operator) {
this.path = path;
this.operator = operator;
this.modifiers = modifiers == null ? Collections.emptySet() : new HashSet<>(modifiers);
this.indices = indices;
}
@Override
public String getPath() {
return path;
}
@Override
public Set<Modifier> getModifiers() {
return Collections.unmodifiableSet(modifiers);
}
@Override
public int[] getIndices() {
return indices;
}
@Override
public Operator getOperator() {
return operator;
}
@Override
public String toString() {
return "(" + path + "," + operator + "," + Arrays.toString(indices) + "," + modifiers + ")";
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/AssignableRepositoryMetadataResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/AssignableRepositoryMetadataResolver.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadata;
import com.mmnaseri.utils.spring.data.error.RepositoryDefinitionException;
import org.springframework.core.GenericTypeResolver;
import org.springframework.data.repository.Repository;
import java.util.Objects;
/**
* This class will try to determine the repository metadata from the generic arguments defined by
* the repository interface, assuming that it has extended the {@link Repository Repository}
* interface from Spring Data Commons.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
public class AssignableRepositoryMetadataResolver extends AbstractRepositoryMetadataResolver {
@Override
protected RepositoryMetadata resolveFromInterface(Class<?> repositoryInterface) {
if (!Repository.class.isAssignableFrom(repositoryInterface)) {
throw new RepositoryDefinitionException(
repositoryInterface, "Expected interface to extend " + Repository.class);
}
final Class<?>[] arguments =
Objects.requireNonNull(
GenericTypeResolver.resolveTypeArguments(repositoryInterface, Repository.class));
final Class<?> entityType = arguments[0];
final Class<?> idType = arguments[1];
final String idProperty = resolveIdProperty(entityType, idType);
return new ImmutableRepositoryMetadata(idType, entityType, repositoryInterface, idProperty);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableOperator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableOperator.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.Matcher;
import com.mmnaseri.utils.spring.data.domain.Operator;
/**
* This is an immutable operator.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class ImmutableOperator implements Operator {
private final String name;
private final int operands;
private final Matcher matcher;
private final String[] tokens;
public ImmutableOperator(String name, int operands, Matcher matcher, String... tokens) {
this.name = name;
this.operands = operands;
this.matcher = matcher;
this.tokens = tokens;
}
public String getName() {
return name;
}
@Override
public int getOperands() {
return operands;
}
@Override
public Matcher getMatcher() {
return matcher;
}
@Override
public String[] getTokens() {
return tokens;
}
@Override
public String toString() {
return name;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/MethodQueryDescriptionExtractor.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/MethodQueryDescriptionExtractor.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.google.common.base.Joiner;
import com.mmnaseri.utils.spring.data.domain.MatchedOperator;
import com.mmnaseri.utils.spring.data.domain.Modifier;
import com.mmnaseri.utils.spring.data.domain.Operator;
import com.mmnaseri.utils.spring.data.domain.OperatorContext;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import com.mmnaseri.utils.spring.data.domain.RepositoryMetadata;
import com.mmnaseri.utils.spring.data.error.QueryParserException;
import com.mmnaseri.utils.spring.data.proxy.RepositoryFactoryConfiguration;
import com.mmnaseri.utils.spring.data.query.NullHandling;
import com.mmnaseri.utils.spring.data.query.Order;
import com.mmnaseri.utils.spring.data.query.PageParameterExtractor;
import com.mmnaseri.utils.spring.data.query.PropertyDescriptor;
import com.mmnaseri.utils.spring.data.query.QueryDescriptor;
import com.mmnaseri.utils.spring.data.query.SortDirection;
import com.mmnaseri.utils.spring.data.query.SortParameterExtractor;
import com.mmnaseri.utils.spring.data.query.impl.DefaultQueryDescriptor;
import com.mmnaseri.utils.spring.data.query.impl.DirectSortParameterExtractor;
import com.mmnaseri.utils.spring.data.query.impl.ImmutableOrder;
import com.mmnaseri.utils.spring.data.query.impl.ImmutableSort;
import com.mmnaseri.utils.spring.data.query.impl.PageablePageParameterExtractor;
import com.mmnaseri.utils.spring.data.query.impl.PageableSortParameterExtractor;
import com.mmnaseri.utils.spring.data.query.impl.WrappedSortParameterExtractor;
import com.mmnaseri.utils.spring.data.string.DocumentReader;
import com.mmnaseri.utils.spring.data.string.impl.DefaultDocumentReader;
import com.mmnaseri.utils.spring.data.tools.PropertyUtils;
import com.mmnaseri.utils.spring.data.tools.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
/**
* This class will parse a query method's name and extract a {@link
* com.mmnaseri.utils.spring.data.dsl.factory.QueryDescription query description} from that name.
*
* <p>In parsing the name, words are considered as being tokens in a camel case name.
*
* <p>Here is how a query method's name is parsed:
*
* <ol>
* <li>We will look at the first word in the name until we reach one of the keywords that says we
* are specifying a limit, or we are going over the criteria defined by the method. This
* prefix will be called the "function name" for the operation defined by the query method. If
* the function name is one of "read", "find", "query", "get", "load", or "select", we will
* set the function name to {@literal null} to indicate that no special function should be
* applied to the result set. We are only looking at the first word to let you be more verbose
* about the purpose of your query (e.g. {@literal
* findAllGreatPeopleByGreatnessGreaterThan(Integer greatness)} will still resolve to the
* function {@literal find}, which will ultimately be returned as {@literal null}
* <li>We will then look for any of these patterns:
* <ul>
* <li>The word {@literal By}, signifying that we are ready to start parsing the query
* criteria
* <li>One of the words {@literal First} or {@literal Top}, signifying that we should look
* for a limit on the number of results returned.
* <li>The word {@literal Distinct}, signifying that the results should include no
* duplicates.
* </ul>
* <li>If the word {@literal First} had appeared, we will see if it is followed by an integer. If
* it is, that will be the limit. If not, a limit of {@literal 1} is assumed.
* <li>If the word {@literal Top} had appeared, we will look for the limit number, which should be
* an integer value.
* <li>At this point, we will continue until we see 'By'. In the above, steps, we will look for
* the keywords in any order, and there can be any words in between. So, {@literal
* getTop5StudentsWhoAreAwesomeDistinct} is the same as {@literal getTop5Distinct}
* <li>Once we reach the word "By", we will read the query in terms of "decision branches".
* Branches are separated using the keyword "Or", and each branch is a series of conjunctions.
* So, while you are separating your conditions with "And", you are in the same branch.
* <li>A single branch consists of the pattern:
* "(Property)(Operator)?((And)(Property)(Operator)?)*". If the operator is missing, "Is" is
* assumed. Properties must match a proper property in the domain object. So, if you have
* "AddressZipPrefix" in your query method name, there must be a property reachable by one of
* the following paths in your domain class (in the given order):
* <ul>
* <li>{@literal addressZipPrefix}
* <li>{@literal addressZip.prefix}
* <li>{@literal address.zipPrefix}
* <li>{@literal address.zip.prefix}
* </ul>
* Note that if you have both the "addressZip" and "address.zip" in your entity, the first
* will be taken up. To force the parser to choose the former, use the underscore character
* ({@literal _}) in place of the dot, like so: "{@literal Address_Zip}"<br>
* Depending on the operator that was matched to the suffix provided (e.g. GreaterThan, Is,
* etc.), a given number of method parameters will be matched as the operands to that
* operator. For instance, "Is" requires two values to determine equality, one if the property
* found on the domain object, and the other must be provided by the query method.<br>
* The operators themselves are scanned eagerly and based on the set of operators defined in
* the {@link OperatorContext}.
* <li>We continue the pattern indicated above, until we reach the end of the method name, or we
* reach the "OrderBy" pattern. Once we see "OrderBy" we expect the following pattern:
* "((Property)(Direction))+", wherein "Property" must follow the same rule as above, and
* "Direction" is one of "Asc" and "Desc" to indicate "ascending" and "descending" ordering,
* respectively.
* <li>Finally, we look to see if the keyword "AllIgnoreCase" or {@link #ALL_IGNORE_CASE_SUFFIX
* one of its variations} is present at the end of the query name, which will indicate all
* applicable comparisons should be case-insensitive.
* <li>At the end, we allow one additional parameter for the query method, which can be of either
* of these types:
* <ul>
* <li>{@link Sort Sort}: to indicate a dynamic sort defined at runtime. If a static sort is
* already indicated via the pattern above, this will result in an error.
* <li>{@link Pageable Pageable}: to indicate a paging (and, possibly, sorting) at runtime.
* If a static sort is already indicated via the pattern above, the sort portion of this
* parameter will be always ignored.
* </ul>
* </ol>
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public class MethodQueryDescriptionExtractor implements QueryDescriptionExtractor<Method> {
private static final String ALL_IGNORE_CASE_SUFFIX =
"(AllIgnoreCase|AllIgnoresCase|AllIgnoringCase)$";
private static final String IGNORE_CASE_PARTIAL = "(IgnoreCase|IgnoresCase|IgnoringCase)";
private static final String ASC_SUFFIX = "Asc";
private static final String DESC_SUFFIX = "Desc";
private static final String DEFAULT_OPERATOR_SUFFIX = "Is";
private final OperatorContext operatorContext;
public MethodQueryDescriptionExtractor(OperatorContext operatorContext) {
this.operatorContext = operatorContext;
}
/**
* Extracts query description from a method's name. This will be done according to {@link
* MethodQueryDescriptionExtractor the parsing rules} for this extractor.
*
* @param repositoryMetadata the repository metadata for this method.
* @param configuration the repository factory configuration. This will be passed down through the
* description.
* @param method the query method
* @return the description for the query
*/
@Override
public QueryDescriptor extract(
RepositoryMetadata repositoryMetadata,
RepositoryFactoryConfiguration configuration,
Method method) {
String methodName = method.getName();
// check to see if the AllIgnoreCase flag is set
boolean allIgnoreCase = methodName.matches(".*" + ALL_IGNORE_CASE_SUFFIX);
// we need to unify method name afterwards
methodName = allIgnoreCase ? methodName.replaceFirst(ALL_IGNORE_CASE_SUFFIX, "") : methodName;
// create a document reader for processing method name
final DocumentReader reader = new DefaultDocumentReader(methodName);
String function = parseFunctionName(method, reader);
final QueryModifiers queryModifiers = parseQueryModifiers(method, reader);
// this is the extractor used for getting paging data
final PageParameterExtractor pageExtractor;
// this is the extractor used for getting sorting data
SortParameterExtractor sortExtractor;
// these are decision branches, each of which denoting an AND clause
final List<List<Parameter>> branches = new ArrayList<>();
// if the method name simply was the function name, no metadata can be extracted
if (!reader.hasMore()) {
pageExtractor = null;
sortExtractor = null;
} else {
reader.expect("By");
if (!reader.hasMore()) {
throw new QueryParserException(
method.getDeclaringClass(), "Query method name cannot end with `By`");
}
// current parameter index
int index = parseExpression(repositoryMetadata, method, allIgnoreCase, reader, branches);
final com.mmnaseri.utils.spring.data.query.Sort sort =
parseSort(repositoryMetadata, method, reader);
pageExtractor = getPageParameterExtractor(method, index, sort);
sortExtractor = getSortParameterExtractor(method, index, sort);
}
return new DefaultQueryDescriptor(
queryModifiers.isDistinct(),
function,
queryModifiers.getLimit(),
pageExtractor,
sortExtractor,
branches(branches),
configuration,
repositoryMetadata);
}
private SortParameterExtractor getSortParameterExtractor(
Method method, int index, com.mmnaseri.utils.spring.data.query.Sort sort) {
SortParameterExtractor sortExtractor = null;
if (method.getParameterTypes().length == index) {
sortExtractor = sort == null ? null : new WrappedSortParameterExtractor(sort);
} else if (method.getParameterTypes().length == index + 1) {
if (Pageable.class.isAssignableFrom(method.getParameterTypes()[index])) {
sortExtractor =
sort == null
? new PageableSortParameterExtractor(index)
: new WrappedSortParameterExtractor(sort);
} else if (Sort.class.isAssignableFrom(method.getParameterTypes()[index])) {
sortExtractor = new DirectSortParameterExtractor(index);
}
}
return sortExtractor;
}
private PageParameterExtractor getPageParameterExtractor(
Method method, int index, com.mmnaseri.utils.spring.data.query.Sort sort) {
PageParameterExtractor pageExtractor;
if (method.getParameterTypes().length == index) {
pageExtractor = null;
} else if (method.getParameterTypes().length == index + 1) {
if (Pageable.class.isAssignableFrom(method.getParameterTypes()[index])) {
pageExtractor = new PageablePageParameterExtractor(index);
} else if (Sort.class.isAssignableFrom(method.getParameterTypes()[index])) {
if (sort != null) {
throw new QueryParserException(
method.getDeclaringClass(),
"You cannot specify both an order-by clause and a dynamic ordering");
}
pageExtractor = null;
} else {
throw new QueryParserException(
method.getDeclaringClass(),
"Invalid last argument: expected paging or sorting " + method);
}
} else {
throw new QueryParserException(
method.getDeclaringClass(), "Too many parameters declared for query method " + method);
}
return pageExtractor;
}
private int parseExpression(
RepositoryMetadata repositoryMetadata,
Method method,
boolean allIgnoreCase,
DocumentReader reader,
List<List<Parameter>> branches) {
int index = 0;
branches.add(new LinkedList<>());
while (reader.hasMore()) {
final Parameter parameter;
// read a full expression
String expression = parseInitialExpression(reader);
// If we have encountered an OrderBy as the first thing in the expression, then we have no
// properties to
// filter on and we only want to order.
if (expression.startsWith("OrderBy")) {
reader.backtrack(expression.length());
break;
}
// if the expression ended in Or, this is the end of this branch
boolean branchEnd = expression.endsWith("Or");
// if the expression contains an OrderBy, it is not only the end of the branch, but also the
// end of the query
boolean expressionEnd = expression.matches(".+[a-z]OrderBy[A-Z].+");
expression = handleExpressionEnd(reader, expression, expressionEnd);
final Set<Modifier> modifiers = new HashSet<>();
expression = parseModifiers(allIgnoreCase, expression, modifiers);
// if the expression ends in And/Or, we expect there to be more
if (expression.matches(".*?(And|Or)$") && !reader.hasMore()) {
throw new QueryParserException(
method.getDeclaringClass(), "Expected more tokens to follow AND/OR operator");
}
expression = expression.replaceFirst("(And|Or)$", "");
String foundProperty = null;
Operator operator = parseOperator(expression);
if (operator != null) {
foundProperty =
expression.substring(
0, expression.length() - ((MatchedOperator) operator).getMatchedToken().length());
}
// if no operator was found, it is the implied "IS" operator
if (operator == null || foundProperty.isEmpty()) {
foundProperty = expression;
operator = operatorContext.getBySuffix(DEFAULT_OPERATOR_SUFFIX);
}
final PropertyDescriptor propertyDescriptor =
getPropertyDescriptor(repositoryMetadata, method, foundProperty);
final String property = propertyDescriptor.getPath();
// we need to match the method parameters with the operands for the designated operator
final int[] indices = new int[operator.getOperands()];
index = parseParameterIndices(method, index, operator, propertyDescriptor, indices);
// create a parameter definition for the given expression
parameter = new ImmutableParameter(property, modifiers, indices, operator);
// get the current branch
final List<Parameter> currentBranch = branches.get(branches.size() - 1);
// add this parameter to the latest branch
currentBranch.add(parameter);
// if the branch has ended with "OR", we set up a new branch
if (branchEnd) {
branches.add(new LinkedList<>());
}
// if this is the end of expression, so we need to jump out
if (expressionEnd) {
break;
}
}
return index;
}
private com.mmnaseri.utils.spring.data.query.Sort parseSort(
RepositoryMetadata repositoryMetadata, Method method, DocumentReader reader) {
final com.mmnaseri.utils.spring.data.query.Sort sort;
// let's figure out if there is a sort requirement embedded in the query definition
if (reader.read("OrderBy") != null) {
final List<Order> orders = new ArrayList<>();
while (reader.hasMore()) {
orders.add(parseOrder(method, reader, repositoryMetadata));
}
sort = new ImmutableSort(orders);
} else {
sort = null;
}
return sort;
}
private int parseParameterIndices(
Method method,
int index,
Operator operator,
PropertyDescriptor propertyDescriptor,
int[] indices) {
int parameterIndex = index;
Class<?>[] parameterTypes = new Class[operator.getOperands()];
for (int i = 0; i < operator.getOperands(); i++) {
if (parameterIndex >= method.getParameterTypes().length) {
throw new QueryParserException(
method.getDeclaringClass(), "Expected to see parameter with index " + parameterIndex);
}
parameterTypes[i] = method.getParameterTypes()[parameterIndex];
indices[i] = parameterIndex++;
}
if (!operator.getMatcher().isApplicableTo(propertyDescriptor.getType(), parameterTypes)) {
throw new QueryParserException(
method.getDeclaringClass(),
String.format(
"Invalid parameter types for operator %s on property %s: [%s]",
operator.getName(),
propertyDescriptor.getPath(),
Joiner.on(",").join(parameterTypes)));
}
return parameterIndex;
}
private PropertyDescriptor getPropertyDescriptor(
RepositoryMetadata repositoryMetadata, Method method, String property) {
// let's get the property descriptor
final PropertyDescriptor propertyDescriptor;
try {
propertyDescriptor =
PropertyUtils.getPropertyDescriptor(repositoryMetadata.getEntityType(), property);
} catch (Exception e) {
throw new QueryParserException(
method.getDeclaringClass(),
"Could not find property `"
+ StringUtils.uncapitalize(property)
+ "` on `"
+ repositoryMetadata.getEntityType()
+ "`",
e);
}
return propertyDescriptor;
}
private Operator parseOperator(String expression) {
Operator operator = null;
// let's find out the operator that covers the longest suffix of the operation
for (int i = 1; i < expression.length(); i++) {
final String suffix = expression.substring(i);
operator = operatorContext.getBySuffix(suffix);
if (operator != null) {
operator = new ImmutableMatchedOperator(operator, suffix);
break;
}
}
return operator;
}
private String parseModifiers(
boolean allIgnoreCase, String originalExpression, Set<Modifier> modifiers) {
String expression = originalExpression;
if (expression.matches(".*" + IGNORE_CASE_PARTIAL + ".*")) {
// if the expression contains IgnoreCase, we need to strip that off
modifiers.add(Modifier.IGNORE_CASE);
expression = expression.replaceFirst(IGNORE_CASE_PARTIAL, "");
} else if (allIgnoreCase) {
// if we had already set "AllIgnoreCase", we will still add the modifier
modifiers.add(Modifier.IGNORE_CASE);
}
return expression;
}
private String handleExpressionEnd(
DocumentReader reader, String originalExpression, boolean expressionEnd) {
String expression = originalExpression;
if (expressionEnd) {
// if that is the case, we need to put back the entirety of the order by clause
int length = expression.length();
expression = expression.replaceFirst("^(.+[a-z])OrderBy[A-Z].+$", "$1");
length -= expression.length();
reader.backtrack(length);
}
return expression;
}
private String parseInitialExpression(DocumentReader reader) {
String expression = reader.expect("(.*?)(And[A-Z]|Or[A-Z]|$)");
if (expression.matches(".*?(And|Or)[A-Z]")) {
// if the expression ended in And/Or, we need to put the one extra character we scanned back
// we scan one extra character because we don't want anything like "Order" to be mistaken for
// "Or"
reader.backtrack(1);
expression = expression.substring(0, expression.length() - 1);
}
return expression;
}
private Order parseOrder(
Method method, DocumentReader reader, RepositoryMetadata repositoryMetadata) {
String expression = reader.expect(".*?(Asc|Desc)");
final SortDirection direction;
if (expression.endsWith(ASC_SUFFIX)) {
direction = SortDirection.ASCENDING;
expression = expression.substring(0, expression.length() - ASC_SUFFIX.length());
} else {
direction = SortDirection.DESCENDING;
expression = expression.substring(0, expression.length() - DESC_SUFFIX.length());
}
final PropertyDescriptor propertyDescriptor;
try {
propertyDescriptor =
PropertyUtils.getPropertyDescriptor(repositoryMetadata.getEntityType(), expression);
} catch (Exception e) {
throw new QueryParserException(
method.getDeclaringClass(),
"Failed to get a property descriptor for expression: " + expression,
e);
}
if (!Comparable.class.isAssignableFrom(propertyDescriptor.getType())) {
throw new QueryParserException(
method.getDeclaringClass(),
"Sort property `"
+ propertyDescriptor.getPath()
+ "` is not comparable in `"
+ method.getName()
+ "`");
}
return new ImmutableOrder(direction, propertyDescriptor.getPath(), NullHandling.DEFAULT);
}
private String parseFunctionName(Method method, DocumentReader reader) {
// the first word in the method name is the function name
String function = reader.read(Pattern.compile("^[a-z]+"));
if (function == null) {
throw new QueryParserException(
method.getDeclaringClass(), "Malformed query method name: " + method);
}
// if the method name is one of the following, it is a simple read, and no function is required
if (Arrays.asList("read", "find", "query", "get", "load", "select").contains(function)) {
function = null;
}
return function;
}
private QueryModifiers parseQueryModifiers(Method method, DocumentReader reader) {
// this is the limit set on the number of items being returned
int limit = 0;
// this is the flag that determines whether or not the result should be sifted for distinct
// values
boolean distinct = false;
// we are still reading the function name if we haven't gotten to `By` and we haven't seen
// any of the magic keywords `First`, `Top`, and `Distinct`.
// scan for words prior to 'By'
while (reader.hasMore() && !reader.has("By")) {
// if the next word is Top, then we are setting a limit
if (reader.has("First")) {
if (limit > 0) {
throw new QueryParserException(
method.getDeclaringClass(),
"There is already a limit of " + limit + " specified for this query: " + method);
}
reader.expect("First");
if (reader.has("\\d+")) {
limit = Integer.parseInt(reader.expect("\\d+"));
} else {
limit = 1;
}
continue;
} else if (reader.has("Top")) {
if (limit > 0) {
throw new QueryParserException(
method.getDeclaringClass(),
"There is already a limit of " + limit + " specified for this query: " + method);
}
reader.expect("Top");
limit = Integer.parseInt(reader.expect("\\d+"));
continue;
} else if (reader.has("Distinct")) {
// if the next word is 'Distinct', we are saying we should return distinct results
if (distinct) {
throw new QueryParserException(
method.getDeclaringClass(),
"You have already stated that this query should return distinct "
+ "items: "
+ method);
}
distinct = true;
}
// we read the words until we reach "By".
reader.expect("[A-Z][a-z]+");
}
return new QueryModifiers(limit, distinct);
}
public OperatorContext getOperatorContext() {
return operatorContext;
}
private static List<List<Parameter>> branches(List<List<Parameter>> original) {
List<List<Parameter>> branches = new ArrayList<>();
for (List<Parameter> branch : original) {
if (!branch.isEmpty()) {
branches.add(branch);
}
}
return branches;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableMatchedOperator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/ImmutableMatchedOperator.java | package com.mmnaseri.utils.spring.data.domain.impl;
import com.mmnaseri.utils.spring.data.domain.MatchedOperator;
import com.mmnaseri.utils.spring.data.domain.Matcher;
import com.mmnaseri.utils.spring.data.domain.Operator;
/**
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (4/17/16, 12:37 PM)
*/
@SuppressWarnings("WeakerAccess")
public class ImmutableMatchedOperator implements MatchedOperator {
private final Operator original;
private final String matchedToken;
public ImmutableMatchedOperator(Operator original, String matchedToken) {
this.original = original;
this.matchedToken = matchedToken;
}
@Override
public String getName() {
return original.getName();
}
@Override
public int getOperands() {
return original.getOperands();
}
@Override
public Matcher getMatcher() {
return original.getMatcher();
}
@Override
public String[] getTokens() {
return original.getTokens();
}
@Override
public String getMatchedToken() {
return matchedToken;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/ConfigurableSequentialLongKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/ConfigurableSequentialLongKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.impl.AbstractRandomKeyGenerator;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (6/14/16, 11:47 AM)
*/
public class ConfigurableSequentialLongKeyGenerator extends AbstractRandomKeyGenerator<Long> {
private final AtomicLong current;
private final long step;
public ConfigurableSequentialLongKeyGenerator() {
this(1L);
}
public ConfigurableSequentialLongKeyGenerator(long initialValue) {
this(initialValue, 1L);
}
public ConfigurableSequentialLongKeyGenerator(long initialValue, long step) {
current = new AtomicLong(initialValue);
this.step = step;
}
@Override
protected Long getNext() {
return current.getAndAdd(step);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/SequentialIntegerKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/SequentialIntegerKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This class will generate sequential numbers.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/6/15)
*/
public class SequentialIntegerKeyGenerator implements KeyGenerator<Integer> {
private final AtomicInteger seed = new AtomicInteger(1);
@Override
public Integer generate() {
return seed.getAndIncrement();
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/RandomIntegerKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/RandomIntegerKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.impl.AbstractRandomKeyGenerator;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
* This class will help with generating unique, random integers.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/6/15)
*/
public class RandomIntegerKeyGenerator extends AbstractRandomKeyGenerator<Integer> {
private final Random random = ThreadLocalRandom.current();
@Override
protected Integer getNext() {
return random.nextInt();
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/NoOpKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/NoOpKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
/**
* This is a key generator that should be used to indicate that we do not want automatic key
* generation.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (6/14/16, 12:51 PM)
*/
public class NoOpKeyGenerator<S> implements KeyGenerator<S> {
@Override
public S generate() {
return null;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/BsonObjectIdKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/BsonObjectIdKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
import org.bson.types.ObjectId;
public class BsonObjectIdKeyGenerator implements KeyGenerator<ObjectId> {
@Override
public ObjectId generate() {
return ObjectId.get();
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/RandomLongKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/RandomLongKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.impl.AbstractRandomKeyGenerator;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
* This class will help with the generation of unique, random long numbers.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/6/15)
*/
public class RandomLongKeyGenerator extends AbstractRandomKeyGenerator<Long> {
private final Random seed = ThreadLocalRandom.current();
@Override
protected Long getNext() {
return seed.nextLong();
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/SequentialLongKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/SequentialLongKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
import java.util.concurrent.atomic.AtomicLong;
/**
* This class will generate sequential, long numbers.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/6/15)
*/
public class SequentialLongKeyGenerator implements KeyGenerator<Long> {
private final AtomicLong seed = new AtomicLong(1);
@Override
public Long generate() {
return seed.getAndIncrement();
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/ConfigurableSequentialIntegerKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/ConfigurableSequentialIntegerKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.impl.AbstractRandomKeyGenerator;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (6/14/16, 11:49 AM)
*/
public class ConfigurableSequentialIntegerKeyGenerator extends AbstractRandomKeyGenerator<Integer> {
private final AtomicInteger current;
private final int step;
public ConfigurableSequentialIntegerKeyGenerator() {
this(1);
}
public ConfigurableSequentialIntegerKeyGenerator(int initialValue) {
this(initialValue, 1);
}
public ConfigurableSequentialIntegerKeyGenerator(int initialValue, int step) {
current = new AtomicInteger(initialValue);
this.step = step;
}
@Override
protected Integer getNext() {
return current.getAndAdd(step);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/UUIDKeyGenerator.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/key/UUIDKeyGenerator.java | package com.mmnaseri.utils.spring.data.domain.impl.key;
import com.mmnaseri.utils.spring.data.domain.KeyGenerator;
import java.util.UUID;
/**
* This class will generate unique UUIDs for use in entities whose keys are loose String values.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (10/6/15)
*/
public class UUIDKeyGenerator implements KeyGenerator<String> {
@Override
public String generate() {
return UUID.randomUUID().toString();
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsBetweenMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsBetweenMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This comparing matcher will determine if the passed value is larger or equal to the first passed
* argument and smaller or equal to the second passed argument, thus determining if it falls between
* the two values.
*
* <p><strong>NB</strong>: This matcher does not check whether or not the two values passed are in
* the right order, as a normal database wouldn't. If you need this functionality, you will need to
* define a new {@link com.mmnaseri.utils.spring.data.domain.Operator operator} and add your own
* matcher.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsBetweenMatcher extends AbstractBinaryComparableMatcher {
@Override
protected boolean matches(Comparable value, Comparable first, Comparable second) {
//noinspection unchecked
return value != null
&& first != null
&& second != null
&& first.compareTo(value) <= 0
&& second.compareTo(value) >= 0;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import java.util.Objects;
/**
* This matcher will determine if the two values are not equal.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public class IsNotMatcher extends AbstractSimpleMatcher {
@Override
protected boolean matches(Parameter parameter, Object actual, Object expected) {
return !Objects.equals(actual, expected);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractSimpleStringMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractSimpleStringMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Modifier;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import com.mmnaseri.utils.spring.data.error.InvalidArgumentException;
/**
* Used when the subject of the operation is a string
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public abstract class AbstractSimpleStringMatcher extends AbstractSimpleMatcher {
@Override
protected final boolean matches(Parameter parameter, Object actual, Object expected) {
if (!(actual instanceof String) || !(expected instanceof String)) {
throw new InvalidArgumentException(
"Expected string values for property: " + parameter.getPath());
}
String first = (String) actual;
String second = (String) expected;
if (parameter.getModifiers().contains(Modifier.IGNORE_CASE)) {
first = first.toLowerCase();
second = second.toLowerCase();
}
return matches(first, second);
}
/**
* Called when we want to check the expectation
*
* @param actual the actual value
* @param argument the argument to the operator
* @return {@literal true} if the match succeeded
*/
protected abstract boolean matches(String actual, String argument);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsEqualToMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsEqualToMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import java.util.Objects;
/**
* This matcher will check to see if the value is strictly equal to the value passed as the argument
* of the query method. This also includes equating null values.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public class IsEqualToMatcher extends AbstractSimpleMatcher {
@Override
protected boolean matches(Parameter parameter, Object actual, Object expected) {
return Objects.equals(actual, expected);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotBetweenMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotBetweenMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This comparing matcher will determine if the passed value is smaller or equal to the first passed
* argument and larger or equal to the second passed argument, thus determining if it does not fall
* between the two values (if they are in in ascending order themselves).
*
* <p><strong>NB</strong>: This matcher does not check whether or not the two values passed are in
* the right order, as a normal database wouldn't. If you need this functionality, you will need to
* define a new {@link com.mmnaseri.utils.spring.data.domain.Operator operator} and add your own
* matcher.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsNotBetweenMatcher extends AbstractBinaryComparableMatcher {
@Override
protected boolean matches(Comparable value, Comparable first, Comparable second) {
//noinspection unchecked
return first != null
&& second != null
&& (value == null || first.compareTo(value) > 0 || second.compareTo(value) < 0);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsLikeMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsLikeMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This matcher checks to see if the two values are the same, barring case differences.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsLikeMatcher extends AbstractSimpleStringMatcher {
@Override
protected boolean matches(String actual, String argument) {
return actual != null && actual.equalsIgnoreCase(argument);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/RegexMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/RegexMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This matcher will determine if the value on the object (a string) matches the pattern being
* passed.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class RegexMatcher extends AbstractSimpleStringMatcher {
@Override
protected boolean matches(String actual, String argument) {
return actual != null && argument != null && actual.matches(argument);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsGreaterThanMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsGreaterThanMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This will check to see if the value on the object is greater than the argument being passed (the
* pivot).
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsGreaterThanMatcher extends AbstractSimpleComparableMatcher {
@Override
protected boolean matches(Comparable actual, Comparable pivot) {
//noinspection unchecked
return actual != null && pivot != null && pivot.compareTo(actual) < 0;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotNullMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotNullMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This matcher will determine if the value on the object is not {@literal null}
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsNotNullMatcher extends AbstractUnaryMatcher {
@Override
protected boolean matches(Object value) {
return value != null;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotLikeMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotLikeMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This matcher will return {@literal true} if the argument passed is not equal to the value on the
* object, even when their case differences are ignored.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsNotLikeMatcher extends AbstractSimpleStringMatcher {
@Override
protected boolean matches(String actual, String argument) {
return (actual == null && argument != null)
|| (actual != null && argument == null)
|| (actual != null && !actual.equalsIgnoreCase(argument));
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNullMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNullMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This matcher will return {@literal true} if the value on the object is {@literal null}
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsNullMatcher extends AbstractUnaryMatcher {
@Override
protected boolean matches(Object value) {
return value == null;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractSimpleMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractSimpleMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Matcher;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import com.mmnaseri.utils.spring.data.error.InvalidArgumentException;
import com.mmnaseri.utils.spring.data.tools.PropertyUtils;
/**
* This matcher is used to determine if a condition holds for a single parameter
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/17/15)
*/
public abstract class AbstractSimpleMatcher implements Matcher {
@Override
public final boolean matches(Parameter parameter, Object value, Object... properties) {
if (properties.length != 1) {
throw new InvalidArgumentException(
"Expected exactly one parameter to be passed down: " + parameter.getPath());
}
return matches(parameter, value, properties[0]);
}
/**
* Called to see if the condition holds
*
* @param parameter the parameter
* @param actual the actual value
* @param expected the expectation
* @return {@literal true} if the condition holds
*/
protected abstract boolean matches(Parameter parameter, Object actual, Object expected);
@Override
public boolean isApplicableTo(Class<?> parameterType, Class<?>... propertiesTypes) {
return propertiesTypes.length == 1
&& PropertyUtils.getTypeOf(parameterType)
.isAssignableFrom(PropertyUtils.getTypeOf(propertiesTypes[0]));
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsInMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsInMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import java.util.Collection;
/**
* This matcher checks to see if the argument being passed (the collection) contains the value on
* the object.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsInMatcher extends AbstractCollectionMatcher {
@Override
protected boolean matches(Object actual, Collection collection) {
return actual != null && collection.contains(actual);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsLessThanMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsLessThanMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This will check to see if the value on the object is less than the argument being passed (the
* pivot).
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsLessThanMatcher extends AbstractSimpleComparableMatcher {
@Override
protected boolean matches(Comparable actual, Comparable pivot) {
//noinspection unchecked
return actual != null && pivot != null && pivot.compareTo(actual) > 0;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsTrueMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsTrueMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This will check to see if the value on the entity object is {@literal true}.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsTrueMatcher extends AbstractUnaryMatcher {
@Override
protected boolean matches(Object value) {
return Boolean.TRUE.equals(value);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/ContainingMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/ContainingMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This class will look for a substring in the passed string value by converting both the needle and
* the haystack to lower case.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class ContainingMatcher extends AbstractSimpleStringMatcher {
@Override
protected boolean matches(String actual, String argument) {
return actual != null
&& argument != null
&& actual.toLowerCase().contains(argument.toLowerCase());
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractCollectionMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractCollectionMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import com.mmnaseri.utils.spring.data.error.InvalidArgumentException;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
/**
* This class paves the way for matching a value against a collection of items
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public abstract class AbstractCollectionMatcher extends AbstractSimpleMatcher {
@Override
protected final boolean matches(Parameter parameter, Object actual, Object property) {
if (property == null) {
throw new InvalidArgumentException(
"Comparison property cannot be null: " + parameter.getPath());
}
final Collection collection;
if (property.getClass().isArray()) {
// if it was an array, we do array to collection conversion, which is pretty straightforward
collection = new LinkedList();
for (int i = 0; i < Array.getLength(property); i++) {
final Object item = Array.get(property, i);
//noinspection unchecked
collection.add(item);
}
} else if (property instanceof Iterator) {
// if it is an iterator, we just iterate it forward
collection = new LinkedList();
final Iterator iterator = (Iterator) property;
while (iterator.hasNext()) {
//noinspection unchecked
collection.add(iterator.next());
}
} else if (property instanceof Iterable) {
// if it is already an iterable object, we just iterate over it.
collection = new LinkedList();
for (Object item : ((Iterable) property)) {
//noinspection unchecked
collection.add(item);
}
} else {
// otherwise, we just don't know how to convert it!
throw new InvalidArgumentException("Expected an array, an iterator, or an iterable object");
}
return matches(actual, collection);
}
/**
* Used to find out if a collection satisfies the condition set forth by this matcher
*
* @param actual the actual value
* @param collection the collection
* @return {@literal true} if the match was a success
*/
protected abstract boolean matches(Object actual, Collection collection);
@Override
public boolean isApplicableTo(Class<?> parameterType, Class<?>... propertiesTypes) {
Class<?> propertyType = propertiesTypes[0];
return Collection.class.isAssignableFrom(propertyType)
|| Iterator.class.isAssignableFrom(propertyType)
|| Iterable.class.isAssignableFrom(propertyType);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractUnaryMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractUnaryMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Matcher;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import com.mmnaseri.utils.spring.data.error.InvalidArgumentException;
/**
* This is used when there is no argument needed to determine the validity of the match
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public abstract class AbstractUnaryMatcher implements Matcher {
@Override
public final boolean matches(Parameter parameter, Object value, Object... properties) {
if (properties.length != 0) {
throw new InvalidArgumentException(
"This operator does not take any operands: "
+ parameter.getOperator().getName()
+ " at "
+ parameter.getPath());
}
return matches(value);
}
/**
* Called to determine the match
*
* @param value the expected value
* @return {@literal true} if the match applies
*/
protected abstract boolean matches(Object value);
@Override
public boolean isApplicableTo(Class<?> parameterType, Class<?>... propertiesTypes) {
return propertiesTypes.length == 0;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractSimpleComparableMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractSimpleComparableMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import com.mmnaseri.utils.spring.data.error.InvalidArgumentException;
/**
* This is used to compare two items. One being the value, and the other the sole parameter.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public abstract class AbstractSimpleComparableMatcher extends AbstractSimpleMatcher {
@Override
protected final boolean matches(Parameter parameter, Object actual, Object expected) {
if (!(actual instanceof Comparable) || !(expected instanceof Comparable)) {
throw new InvalidArgumentException(
"Expected property to be comparable: " + parameter.getPath());
}
return matches((Comparable) actual, (Comparable) expected);
}
/**
* Does comparison and returns the result.
*
* @param actual the actual value
* @param pivot the pivot
* @return {@literal true} if the match succeeded
*/
protected abstract boolean matches(Comparable actual, Comparable pivot);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractBinaryMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractBinaryMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Matcher;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import com.mmnaseri.utils.spring.data.error.InvalidArgumentException;
import com.mmnaseri.utils.spring.data.tools.PropertyUtils;
/**
* Used for matching operands to a binary operator
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public abstract class AbstractBinaryMatcher implements Matcher {
@Override
public final boolean matches(Parameter parameter, Object value, Object... properties) {
if (properties.length != 2) {
throw new InvalidArgumentException(
"Expected two values to be passed to operator "
+ parameter.getOperator().getName()
+ " at "
+ parameter.getPath());
}
return matches(parameter, value, properties[0], properties[1]);
}
/**
* Called to see if two objects match the criteria set by this matcher
*
* @param parameter the parameter against which the matching is being performed
* @param value the value bound to the matching
* @param first the first operand
* @param second the second operand
* @return {@literal true} if it was a match
*/
protected abstract boolean matches(
Parameter parameter, Object value, Object first, Object second);
@Override
public boolean isApplicableTo(Class<?> parameterType, Class<?>... propertiesTypes) {
return propertiesTypes.length == 2
&& PropertyUtils.getTypeOf(parameterType)
.isAssignableFrom(PropertyUtils.getTypeOf(propertiesTypes[0]))
&& PropertyUtils.getTypeOf(parameterType)
.isAssignableFrom(PropertyUtils.getTypeOf(propertiesTypes[1]));
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractBinaryComparableMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/AbstractBinaryComparableMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import com.mmnaseri.utils.spring.data.domain.Parameter;
import com.mmnaseri.utils.spring.data.error.InvalidArgumentException;
/**
* This class is the base class used for doing binary operations when both operands are {@link
* Comparable Comparable} objects
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public abstract class AbstractBinaryComparableMatcher extends AbstractBinaryMatcher {
@Override
protected final boolean matches(Parameter parameter, Object value, Object first, Object second) {
if (!(value instanceof Comparable)
|| !(first instanceof Comparable)
|| !(second instanceof Comparable)) {
throw new InvalidArgumentException(
"Expected values to be comparable: " + parameter.getPath());
}
return matches((Comparable) value, (Comparable) first, (Comparable) second);
}
/**
* Is called to determine when the two comparable items fit the criteria of this matcher
*
* @param value the value against which the comparison is being performed
* @param first the first value
* @param second the second value
* @return {@literal true} if it was a match
*/
protected abstract boolean matches(Comparable value, Comparable first, Comparable second);
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsGreaterThanOrEqualToMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsGreaterThanOrEqualToMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This will check to see if the value on the object is greater than or equal to the argument being
* passed (the pivot).
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsGreaterThanOrEqualToMatcher extends AbstractSimpleComparableMatcher {
@Override
protected boolean matches(Comparable actual, Comparable pivot) {
//noinspection unchecked
return actual != null && pivot != null && pivot.compareTo(actual) <= 0;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/EndingWithMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/EndingWithMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This class is used to find out if the given value ends with the passed argument. Remember that
* this will convert both the needle and the haystack to lower case, so the search is
* case-insensitive.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class EndingWithMatcher extends AbstractSimpleStringMatcher {
@Override
protected boolean matches(String actual, String argument) {
return actual != null
&& argument != null
&& actual.toLowerCase().endsWith(argument.toLowerCase());
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotInMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsNotInMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
import java.util.Collection;
/**
* This matcher checks whether or not the argument passed to the query method (the collection)
* contains the value on the object itself and fails the check if it does.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsNotInMatcher extends AbstractCollectionMatcher {
@Override
protected boolean matches(Object actual, Collection collection) {
return !collection.contains(actual);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsFalseMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsFalseMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This will check to see if the value on the entity object is {@literal false}.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsFalseMatcher extends AbstractUnaryMatcher {
@Override
protected boolean matches(Object value) {
return Boolean.FALSE.equals(value);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/StartingWithMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/StartingWithMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This class is used to find out if the given value starts with the passed argument. Remember that
* this will convert both the needle and the haystack to lower case, so the search is
* case-insensitive.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class StartingWithMatcher extends AbstractSimpleStringMatcher {
@Override
protected boolean matches(String actual, String argument) {
return actual != null
&& argument != null
&& actual.toLowerCase().startsWith(argument.toLowerCase());
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsLessThanOrEqualToMatcher.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/matchers/IsLessThanOrEqualToMatcher.java | package com.mmnaseri.utils.spring.data.domain.impl.matchers;
/**
* This will check to see if the value on the object is less than the or equal to argument being
* passed (the pivot).
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/29/15)
*/
public class IsLessThanOrEqualToMatcher extends AbstractSimpleComparableMatcher {
@Override
protected boolean matches(Comparable actual, Comparable pivot) {
//noinspection unchecked
return actual != null && pivot != null && pivot.compareTo(actual) >= 0;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/id/IdPropertyResolverUtils.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/id/IdPropertyResolverUtils.java | package com.mmnaseri.utils.spring.data.domain.impl.id;
import com.mmnaseri.utils.spring.data.error.PropertyTypeMismatchException;
import com.mmnaseri.utils.spring.data.tools.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ClassUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* Utilities for figuring out the specifics of the ID property
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (6/8/16, 1:43 AM)
*/
@SuppressWarnings("WeakerAccess")
public final class IdPropertyResolverUtils {
private static final List<String> ID_ANNOTATIONS = new ArrayList<>();
private static final Log log = LogFactory.getLog(IdPropertyResolverUtils.class);
static {
ID_ANNOTATIONS.add("org.springframework.data.annotation.Id");
ID_ANNOTATIONS.add("javax.persistence.Id");
ID_ANNOTATIONS.add("javax.persistence.EmbeddedId");
}
private IdPropertyResolverUtils() {
throw new UnsupportedOperationException();
}
/**
* Returns the name of the property as represented by the method given
*
* @param entityType the type of the entity that the ID is being resolved for
* @param idType the type of the ID expected for the entity
* @param idAnnotatedMethod the method that will return the ID (e.g. getter for the ID property)
* @return the name of the property, or {@literal null} if the method is {@literal null}
*/
public static String getPropertyNameFromAnnotatedMethod(
Class<?> entityType, Class<?> idType, Method idAnnotatedMethod) {
if (idAnnotatedMethod != null) {
final String name = PropertyUtils.getPropertyName(idAnnotatedMethod);
if (!PropertyUtils.getTypeOf(idType)
.isAssignableFrom(PropertyUtils.getTypeOf(idAnnotatedMethod.getReturnType()))) {
throw new PropertyTypeMismatchException(
entityType, name, idType, idAnnotatedMethod.getReturnType());
} else {
return name;
}
}
return null;
}
/**
* Determines whether or not the given element is annotated with the annotations specified by
* {@link #getIdAnnotations()}
*
* @param element the element to be examined
* @return {@literal true} if the element has any of the ID annotations
*/
public static boolean isAnnotated(AnnotatedElement element) {
final List<Class<? extends Annotation>> annotations = getIdAnnotations();
for (Class<? extends Annotation> annotation : annotations) {
if (AnnotationUtils.findAnnotation(element, annotation) != null) {
return true;
}
}
return false;
}
/**
* Lists all the annotations that can be used to mark a property as the ID property based on the
* libraries that can be found in the classpath
*
* @return the list of annotations
*/
private static List<Class<? extends Annotation>> getIdAnnotations() {
final List<Class<? extends Annotation>> annotations = new ArrayList<>();
final ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
for (String idAnnotation : ID_ANNOTATIONS) {
try {
final Class<?> type = ClassUtils.forName(idAnnotation, classLoader);
final Class<? extends Annotation> annotationType = type.asSubclass(Annotation.class);
annotations.add(annotationType);
} catch (ClassNotFoundException ignored) {
// if the class for the annotation wasn't found, we just ignore it
log.debug(
"Requested ID annotation type " + idAnnotation + " is not present in the classpath");
}
}
return annotations;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/id/AnnotatedGetterIdPropertyResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/id/AnnotatedGetterIdPropertyResolver.java | package com.mmnaseri.utils.spring.data.domain.impl.id;
import com.mmnaseri.utils.spring.data.domain.IdPropertyResolver;
import com.mmnaseri.utils.spring.data.error.MultipleIdPropertiesException;
import com.mmnaseri.utils.spring.data.tools.GetterMethodFilter;
import org.springframework.data.annotation.Id;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicReference;
import static com.mmnaseri.utils.spring.data.domain.impl.id.IdPropertyResolverUtils.getPropertyNameFromAnnotatedMethod;
import static com.mmnaseri.utils.spring.data.domain.impl.id.IdPropertyResolverUtils.isAnnotated;
/**
* This class will resolve ID property name from a getter method that is annotated with {@link
* Id @Id}.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
@SuppressWarnings("WeakerAccess")
public class AnnotatedGetterIdPropertyResolver implements IdPropertyResolver {
@Override
public String resolve(final Class<?> entityType, Class<?> idType) {
final AtomicReference<Method> found = new AtomicReference<>();
ReflectionUtils.doWithMethods(
entityType,
method -> {
if (isAnnotated(method)) {
if (found.get() == null) {
found.set(method);
} else {
throw new MultipleIdPropertiesException(entityType);
}
}
},
new GetterMethodFilter());
final Method idAnnotatedMethod = found.get();
return getPropertyNameFromAnnotatedMethod(entityType, idType, idAnnotatedMethod);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/id/NamedGetterIdPropertyResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/id/NamedGetterIdPropertyResolver.java | package com.mmnaseri.utils.spring.data.domain.impl.id;
import com.mmnaseri.utils.spring.data.domain.IdPropertyResolver;
import com.mmnaseri.utils.spring.data.tools.GetterMethodFilter;
import com.mmnaseri.utils.spring.data.tools.PropertyUtils;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicReference;
import static com.mmnaseri.utils.spring.data.domain.impl.id.IdPropertyResolverUtils.getPropertyNameFromAnnotatedMethod;
/**
* This class is for resolving an ID based on the getter. It will try to find a getter for a
* property named {@literal "id"} -- i.e., it will look for a getter named "getId".
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
@SuppressWarnings("WeakerAccess")
public class NamedGetterIdPropertyResolver implements IdPropertyResolver {
@Override
public String resolve(Class<?> entityType, final Class<?> idType) {
final AtomicReference<Method> found = new AtomicReference<>();
ReflectionUtils.doWithMethods(
entityType,
method -> {
if (PropertyUtils.getPropertyName(method).equals("id")) {
found.set(method);
}
},
new GetterMethodFilter());
final Method idAnnotatedMethod = found.get();
return getPropertyNameFromAnnotatedMethod(entityType, idType, idAnnotatedMethod);
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
mmnaseri/spring-data-mock | https://github.com/mmnaseri/spring-data-mock/blob/e8547729050c9454872a1038c23bb9b1288c8654/spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/id/NamedFieldIdPropertyResolver.java | spring-data-mock/src/main/java/com/mmnaseri/utils/spring/data/domain/impl/id/NamedFieldIdPropertyResolver.java | package com.mmnaseri.utils.spring.data.domain.impl.id;
import com.mmnaseri.utils.spring.data.domain.IdPropertyResolver;
import com.mmnaseri.utils.spring.data.error.PropertyTypeMismatchException;
import com.mmnaseri.utils.spring.data.tools.PropertyUtils;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
/**
* This class is for finding a field with the name {@literal "id"}.
*
* @author Milad Naseri (m.m.naseri@gmail.com)
* @since 1.0 (9/23/15)
*/
@SuppressWarnings("WeakerAccess")
public class NamedFieldIdPropertyResolver implements IdPropertyResolver {
@Override
public String resolve(Class<?> entityType, Class<?> idType) {
final Field field = ReflectionUtils.findField(entityType, "id");
if (field != null) {
if (PropertyUtils.getTypeOf(idType)
.isAssignableFrom(PropertyUtils.getTypeOf(field.getType()))) {
return field.getName();
} else {
throw new PropertyTypeMismatchException(
entityType, field.getName(), idType, field.getType());
}
}
return null;
}
}
| java | MIT | e8547729050c9454872a1038c23bb9b1288c8654 | 2026-01-05T02:41:15.897176Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.