Skip to content

Commit

Permalink
DATAGEODE-4 - Adapt to API changes in repository interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm authored and jxblum committed May 3, 2017
1 parent 2f34d88 commit d018192
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 182 deletions.
Expand Up @@ -15,8 +15,6 @@
*/
package org.springframework.data.gemfire.repository;

import java.io.Serializable;

import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;

Expand All @@ -28,8 +26,7 @@
* @see java.io.Serializable
* @see org.springframework.data.repository.CrudRepository
*/
@SuppressWarnings("unused")
public interface GemfireRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
public interface GemfireRepository<T, ID> extends CrudRepository<T, ID> {

/**
* Returns all entities sorted by the given options.
Expand Down
Expand Up @@ -15,88 +15,17 @@
*/
package org.springframework.data.gemfire.repository;

import java.io.Serializable;

import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import lombok.NonNull;
import lombok.Value;

/**
* Simple value object to hold an entity alongside an external key the entity shall be stored under.
*
* @author Oliver Gierke
*/
public final class Wrapper<T, KEY extends Serializable> {

private final KEY key;
private final T entity;

/**
* The entity to handle as well as the key.
*
* @param entity the application domain object/entity to wrap.
* @param key must not be {@literal null}.
*/
public Wrapper(T entity, KEY key) {
Assert.notNull(key);

this.entity = entity;
this.key = key;
}

/**
* @return the key
*/
public KEY getKey() {
return key;
}

/**
* @return the entity
*/
public T getEntity() {
return entity;
}

/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object value) {
if (value == this) {
return true;
}

if (!(value instanceof Wrapper)) {
return false;
}

Wrapper<?, ?> that = (Wrapper<?, ?>) value;

return (this.key.equals(that.key) && ObjectUtils.nullSafeEquals(this.entity, that.entity));
}

/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int result = 17;

result += 31 * key.hashCode();
result += 31 * ObjectUtils.nullSafeHashCode(entity);

return result;
}

/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return ObjectUtils.nullSafeToString(getEntity());
}
@Value
public class Wrapper<T, KEY> {

T entity;
@NonNull KEY key;
}
Expand Up @@ -16,8 +16,6 @@

package org.springframework.data.gemfire.repository.query;

import java.io.Serializable;

import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.repository.core.support.PersistentEntityInformation;
Expand All @@ -32,7 +30,7 @@
* @see org.springframework.data.gemfire.repository.query.GemfireEntityInformation
* @see org.springframework.data.repository.core.support.PersistentEntityInformation
*/
public class DefaultGemfireEntityInformation<T, ID extends Serializable> extends PersistentEntityInformation<T, ID>
public class DefaultGemfireEntityInformation<T, ID> extends PersistentEntityInformation<T, ID>
implements GemfireEntityInformation<T, ID> {

private final GemfirePersistentEntity<T> entity;
Expand Down
Expand Up @@ -15,8 +15,6 @@
*/
package org.springframework.data.gemfire.repository.query;

import java.io.Serializable;

import org.apache.geode.cache.Region;
import org.springframework.data.repository.core.EntityInformation;

Expand All @@ -25,7 +23,7 @@
*
* @author Oliver Gierke
*/
public interface GemfireEntityInformation<T, ID extends Serializable> extends EntityInformation<T, ID> {
public interface GemfireEntityInformation<T, ID> extends EntityInformation<T, ID> {

/**
* Returns the name of the {@link Region} the entity is held in.
Expand Down
Expand Up @@ -16,7 +16,7 @@

package org.springframework.data.gemfire.repository.support;

import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.*;

import java.io.Serializable;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -81,7 +81,8 @@ public GemfireRepositoryFactory(Iterable<Region<?, ?>> regions,
*/
@Override
@SuppressWarnings("unchecked")
public <T, ID extends Serializable> GemfireEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
public <T, ID> GemfireEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {

GemfirePersistentEntity<T> entity = (GemfirePersistentEntity<T>) mappingContext.getPersistentEntity(domainClass)
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]", domainClass));

Expand Down
Expand Up @@ -16,10 +16,7 @@

package org.springframework.data.gemfire.repository.support;

import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand All @@ -41,6 +38,8 @@
import org.springframework.data.gemfire.repository.query.QueryString;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -81,8 +80,8 @@ public SimpleGemfireRepository(GemfireTemplate template, EntityInformation<T, ID
*/
@Override
public <U extends T> U save(U entity) {
ID id = entityInformation.getId(entity).orElseThrow(
() -> newIllegalArgumentException("ID for entity [%s] is required", entity));

ID id = entityInformation.getRequiredId(entity);

template.put(id, entity);

Expand All @@ -94,16 +93,14 @@ public <U extends T> U save(U entity) {
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
*/
@Override
public <U extends T> Iterable<U> save(Iterable<U> entities) {
public <U extends T> Iterable<U> saveAll(Iterable<U> entities) {

Map<ID, U> entitiesToSave = new HashMap<>();

for (U entity : entities) {
ID id = entityInformation.getId(entity).orElseThrow(
() -> newIllegalArgumentException("ID for entity [%s] is required", entity));

entitiesToSave.put(id, entity);
}

entities.forEach(entity -> {
entitiesToSave.put(entityInformation.getRequiredId(entity), entity);
});

template.putAll(entitiesToSave);

return entitiesToSave.values();
Expand Down Expand Up @@ -134,20 +131,19 @@ public long count() {

/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
* @see org.springframework.data.repository.CrudRepository#existsById(java.lang.Object)
*/
@Override
public boolean exists(ID id) {
return findOne(id).isPresent();
public boolean existsById(ID id) {
return findById(id).isPresent();
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
* @see org.springframework.data.repository.CrudRepository#findById(java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public Optional<T> findOne(ID id) {
public Optional<T> findById(ID id) {
return Optional.ofNullable(template.get(id));
}

Expand Down Expand Up @@ -180,27 +176,23 @@ public Iterable<T> findAll(Sort sort) {

/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
* @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable)
*/
@Override
@SuppressWarnings("unchecked")
public Collection<T> findAll(Iterable<ID> ids) {
List<ID> parameters = new ArrayList<>();

for (ID id : ids) {
parameters.add(id);
}
public Collection<T> findAllById(Iterable<ID> ids) {

List<ID> parameters = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList());

return CollectionUtils.<ID, T>nullSafeMap(template.getAll(parameters)).values().stream()
.filter(Objects::nonNull).collect(Collectors.toList());
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
* @see org.springframework.data.repository.CrudRepository#deleteById(java.lang.Object)
*/
@Override
public void delete(ID id) {
public void deleteById(ID id) {
template.remove(id);
}

Expand All @@ -210,26 +202,24 @@ public void delete(ID id) {
*/
@Override
public void delete(T entity) {
delete(entityInformation.getId(entity).orElseThrow(() -> new IllegalArgumentException("ID is required")));
deleteById(entityInformation.getRequiredId(entity));
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
*/
@Override
public void delete(Iterable<? extends T> entities) {
for (T entity : entities) {
delete(entity);
}
public void deleteAll(Iterable<? extends T> entities) {
entities.forEach(this::delete);
}

/*
* (non-Javadoc)
* @see org.apache.geode.cache.Region#getAttributes()
* @see org.apache.geode.cache.RegionAttributes#getDataPolicy()
*/
boolean isPartitioned(Region region) {
boolean isPartitioned(Region<?, ?> region) {
return (region != null && region.getAttributes() != null
&& isPartitioned(region.getAttributes().getDataPolicy()));
}
Expand All @@ -247,7 +237,7 @@ boolean isPartitioned(DataPolicy dataPolicy) {
* @see org.apache.geode.cache.Region#getRegionService()
* @see org.apache.geode.cache.Cache#getCacheTransactionManager()
*/
boolean isTransactionPresent(Region region) {
boolean isTransactionPresent(Region<?, ?> region) {
return (region.getRegionService() instanceof Cache
&& isTransactionPresent(((Cache) region.getRegionService()).getCacheTransactionManager()));
}
Expand All @@ -261,8 +251,7 @@ boolean isTransactionPresent(CacheTransactionManager cacheTransactionManager) {
}

/* (non-Javadoc) */
@SuppressWarnings("unchecked")
void doRegionClear(Region region) {
<K> void doRegionClear(Region<K, ?> region) {
region.removeAll(region.keySet());
}

Expand Down
Expand Up @@ -12,7 +12,7 @@
*/
package org.springframework.data.gemfire.client;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -82,7 +82,6 @@ protected void assertRegion(Region<?, ?> region, String name, String fullPath, D
}

@Test
@SuppressWarnings("unused")
public void gemfireServerDataSourceCreated() {
Pool pool = applicationContext.getBean("gemfirePool", Pool.class);

Expand All @@ -108,7 +107,7 @@ public void repositoryCreatedAndFunctional() {

assertThat(repository.save(daveMathews)).isSameAs(daveMathews);

Optional<Person> result = repository.findOne(1L);
Optional<Person> result = repository.findById(1L);

assertThat(result.isPresent()).isTrue();
assertThat(result.get().getFirstname()).isEqualTo("Dave");
Expand Down
Expand Up @@ -49,7 +49,7 @@ public Person newPerson(String firstName, String lastName) {
}

public Person find(Long id) {
return getPersonRepository().findOne(id).orElse(null);
return getPersonRepository().findById(id).orElse(null);
}

public Person save(Person person) {
Expand Down

0 comments on commit d018192

Please sign in to comment.