Skip to content
This repository has been archived by the owner on Jan 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #31 from Karumi/marvel-example
Browse files Browse the repository at this point in the history
Marvel example
  • Loading branch information
flipper83 committed Dec 22, 2015
2 parents 20c6148 + a03b6eb commit 583b1cc
Show file tree
Hide file tree
Showing 93 changed files with 1,904 additions and 911 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ target
.DS_Store
Thumbs.db
*.swp
*.bak
*.bak
marvel.properties
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ buildscript {

allprojects {
repositories {
mavenCentral()
jcenter()
}

Expand Down
41 changes: 41 additions & 0 deletions rosie/src/main/java/com/karumi/rosie/mapper/Mapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2015 Karumi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.karumi.rosie.mapper;

import java.util.ArrayList;
import java.util.Collection;

public abstract class Mapper<T1, T2> {
public abstract T2 map(T1 value);
public abstract T1 reverseMap(T2 value);

public Collection<T2> map(Collection<T1> values) {
Collection<T2> returnValues = new ArrayList<>();
for (T1 value : values) {
returnValues.add(map(value));
}
return returnValues;
}

public Collection<T1> reverseMap(Collection<T2> values) {
Collection<T1> returnValues = new ArrayList<>();
for (T2 value : values) {
returnValues.add(reverseMap(value));
}
return returnValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
*/
public class PaginatedRosieRepository<K, V extends Identifiable<K>> extends RosieRepository<K, V> {

private final Collection<PaginatedReadableDataSource<V>> paginatedReadableDataSources =
private final Collection<PaginatedReadableDataSource<K, V>> paginatedReadableDataSources =
new LinkedList<>();
private final Collection<PaginatedCacheDataSource<K, V>> paginatedCacheDataSources =
new LinkedList<>();

@SafeVarargs
protected final <R extends PaginatedReadableDataSource<V>> void addPaginatedReadableDataSources(
protected final <R extends PaginatedReadableDataSource<K, V>> void addPaginatedReadableDataSources(
R... readables) {
this.paginatedReadableDataSources.addAll(Arrays.asList(readables));
}
Expand All @@ -52,7 +52,7 @@ protected final <R extends PaginatedCacheDataSource<K, V>> void addPaginatedCach
*
* @param page Page to be retrieved
*/
public PaginatedCollection<V> getPage(Page page) {
public PaginatedCollection<V> getPage(Page page) throws Exception {
return getPage(page, ReadPolicy.READ_ALL);
}

Expand All @@ -62,7 +62,7 @@ public PaginatedCollection<V> getPage(Page page) {
* @param page Page to be retrieved
* @param policy Specifies how the value is going to be retrieved.
*/
public PaginatedCollection<V> getPage(Page page, ReadPolicy policy) {
public PaginatedCollection<V> getPage(Page page, ReadPolicy policy) throws Exception {
PaginatedCollection<V> values = null;

if (policy.useCache()) {
Expand All @@ -80,7 +80,7 @@ public PaginatedCollection<V> getPage(Page page, ReadPolicy policy) {
return values;
}

protected PaginatedCollection<V> getPaginatedValuesFromCaches(Page page) {
protected PaginatedCollection<V> getPaginatedValuesFromCaches(Page page) throws Exception {
PaginatedCollection<V> values = null;

for (PaginatedCacheDataSource<K, V> cacheDataSource : paginatedCacheDataSources) {
Expand All @@ -99,10 +99,10 @@ protected PaginatedCollection<V> getPaginatedValuesFromCaches(Page page) {
return values;
}

protected PaginatedCollection<V> getPaginatedValuesFromReadables(Page page) {
protected PaginatedCollection<V> getPaginatedValuesFromReadables(Page page) throws Exception {
PaginatedCollection<V> values = null;

for (PaginatedReadableDataSource<V> readable : paginatedReadableDataSources) {
for (PaginatedReadableDataSource<K, V> readable : paginatedReadableDataSources) {
values = readable.getPage(page);

if (values != null) {
Expand All @@ -113,7 +113,8 @@ protected PaginatedCollection<V> getPaginatedValuesFromReadables(Page page) {
return values;
}

protected void populatePaginatedCaches(Page page, PaginatedCollection<V> values) {
protected void populatePaginatedCaches(Page page, PaginatedCollection<V> values)
throws Exception {
for (PaginatedCacheDataSource<K, V> cacheDataSource : paginatedCacheDataSources) {
cacheDataSource.addOrUpdatePage(page, values.getItems(), values.hasMore());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class RosieRepository<K, V extends Identifiable<K>>
/**
* {@link ReadableDataSource#getByKey(Object)}
*/
@Override public V getByKey(K key) {
@Override public V getByKey(K key) throws Exception {
return getByKey(key, ReadPolicy.READ_ALL);
}

Expand All @@ -70,7 +70,7 @@ public class RosieRepository<K, V extends Identifiable<K>>
*
* @param policy Specifies how the value is going to be retrieved.
*/
public V getByKey(K key, ReadPolicy policy) {
public V getByKey(K key, ReadPolicy policy) throws Exception {
validateKey(key);

V value = null;
Expand All @@ -93,7 +93,7 @@ public V getByKey(K key, ReadPolicy policy) {
/**
* {@link ReadableDataSource#getAll()}
*/
@Override public Collection<V> getAll() {
@Override public Collection<V> getAll() throws Exception {
return getAll(ReadPolicy.READ_ALL);
}

Expand All @@ -102,7 +102,7 @@ public V getByKey(K key, ReadPolicy policy) {
*
* @param policy Specifies how the value is going to be retrieved.
*/
public Collection<V> getAll(ReadPolicy policy) {
public Collection<V> getAll(ReadPolicy policy) throws Exception {
Collection<V> values = null;

if (policy.useCache()) {
Expand All @@ -123,7 +123,7 @@ public Collection<V> getAll(ReadPolicy policy) {
/**
* {@link WriteableDataSource#addOrUpdate(Identifiable)}
*/
@Override public V addOrUpdate(V value) {
@Override public V addOrUpdate(V value) throws Exception {
return addOrUpdate(value, WritePolicy.WRITE_ALL);
}

Expand All @@ -132,7 +132,7 @@ public Collection<V> getAll(ReadPolicy policy) {
*
* @param policy Specifies how the value is going to be stored.
*/
public V addOrUpdate(V value, WritePolicy policy) {
public V addOrUpdate(V value, WritePolicy policy) throws Exception {
validateValue(value);

V updatedValue = null;
Expand All @@ -155,11 +155,11 @@ public V addOrUpdate(V value, WritePolicy policy) {
/**
* {@link WriteableDataSource#addOrUpdateAll(Collection)}
*/
@Override public Collection<V> addOrUpdateAll(Collection<V> values) {
@Override public Collection<V> addOrUpdateAll(Collection<V> values) throws Exception {
return addOrUpdateAll(values, WritePolicy.WRITE_ALL);
}

public Collection<V> addOrUpdateAll(Collection<V> values, WritePolicy policy) {
public Collection<V> addOrUpdateAll(Collection<V> values, WritePolicy policy) throws Exception {
validateValues(values);

Collection<V> updatedValues = null;
Expand All @@ -182,7 +182,7 @@ public Collection<V> addOrUpdateAll(Collection<V> values, WritePolicy policy) {
/**
* {@link WriteableDataSource#deleteByKey(Object)}
*/
@Override public void deleteByKey(K key) {
@Override public void deleteByKey(K key) throws Exception {
for (WriteableDataSource<K, V> writeableDataSource : writeableDataSources) {
writeableDataSource.deleteByKey(key);
}
Expand All @@ -195,7 +195,7 @@ public Collection<V> addOrUpdateAll(Collection<V> values, WritePolicy policy) {
/**
* {@link WriteableDataSource#deleteAll()}
*/
@Override public void deleteAll() {
@Override public void deleteAll() throws Exception {
for (WriteableDataSource<K, V> writeableDataSource : writeableDataSources) {
writeableDataSource.deleteAll();
}
Expand All @@ -205,7 +205,7 @@ public Collection<V> addOrUpdateAll(Collection<V> values, WritePolicy policy) {
}
}

private V getValueFromCaches(K id) {
private V getValueFromCaches(K id) throws Exception {
V value = null;

for (CacheDataSource<K, V> cacheDataSource : cacheDataSources) {
Expand All @@ -224,7 +224,7 @@ private V getValueFromCaches(K id) {
return value;
}

private Collection<V> getValuesFromCaches() {
private Collection<V> getValuesFromCaches() throws Exception {
Collection<V> values = null;

for (CacheDataSource<K, V> cacheDataSource : cacheDataSources) {
Expand All @@ -243,7 +243,7 @@ private Collection<V> getValuesFromCaches() {
return values;
}

private V getValueFromReadables(K key) {
private V getValueFromReadables(K key) throws Exception {
V value = null;

for (ReadableDataSource<K, V> readableDataSource : readableDataSources) {
Expand All @@ -257,7 +257,7 @@ private V getValueFromReadables(K key) {
return value;
}

protected Collection<V> getValuesFromReadables() {
protected Collection<V> getValuesFromReadables() throws Exception {
Collection<V> values = null;

for (ReadableDataSource<K, V> readableDataSource : readableDataSources) {
Expand All @@ -271,13 +271,13 @@ protected Collection<V> getValuesFromReadables() {
return values;
}

private void populateCaches(V value) {
private void populateCaches(V value) throws Exception {
for (CacheDataSource<K, V> cacheDataSource : cacheDataSources) {
cacheDataSource.addOrUpdate(value);
}
}

protected void populateCaches(Collection<V> values) {
protected void populateCaches(Collection<V> values) throws Exception {
for (CacheDataSource<K, V> cacheDataSource : cacheDataSources) {
cacheDataSource.addOrUpdateAll(values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@
*/
public class EmptyCacheDataSource<K, V extends Identifiable<K>> implements CacheDataSource<K, V> {

@Override public V getByKey(K key) {
@Override public V getByKey(K key) throws Exception {
return null;
}

@Override public Collection<V> getAll() {
@Override public Collection<V> getAll() throws Exception {
return null;
}

@Override public V addOrUpdate(V value) {
@Override public V addOrUpdate(V value) throws Exception {
return null;
}

@Override public Collection<V> addOrUpdateAll(Collection<V> values) {
@Override public Collection<V> addOrUpdateAll(Collection<V> values) throws Exception {
return null;
}

@Override public void deleteByKey(K key) {
@Override public void deleteByKey(K key) throws Exception {

}

@Override public void deleteAll() {
@Override public void deleteAll() throws Exception {

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2015 Karumi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.karumi.rosie.repository.datasource;

import com.karumi.rosie.repository.PaginatedCollection;
import com.karumi.rosie.repository.datasource.paginated.Page;
import com.karumi.rosie.repository.datasource.paginated.PaginatedReadableDataSource;

/**
* Utility class to only override methods that are needed by your readable data source
* implementation.
*/
public class EmptyPaginatedReadableDataSource<K, V> extends EmptyReadableDataSource<K, V>
implements PaginatedReadableDataSource<K, V> {

@Override public PaginatedCollection<V> getPage(Page page) throws Exception {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
*/
public class EmptyReadableDataSource<K, V> implements ReadableDataSource<K, V> {

@Override public V getByKey(K key) {
@Override public V getByKey(K key) throws Exception {
return null;
}

@Override public Collection<V> getAll() {
@Override public Collection<V> getAll() throws Exception {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
public class InMemoryCacheDataSource<K, V extends Identifiable<K>>
implements CacheDataSource<K, V> {

private final TimeProvider timeProvider;
private final long ttlInMillis;
private final List<V> items;
protected final TimeProvider timeProvider;
protected final long ttlInMillis;
protected final List<V> items;

private long lastItemsUpdate;
protected long lastItemsUpdate;

public InMemoryCacheDataSource(TimeProvider timeProvider, long ttlInMillis) {
this.timeProvider = timeProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public interface ReadableDataSource<K, V> {
* @param key The key that uniquely identifies the requested value.
* @return The value associated to the provided key or null if there is not any.
*/
V getByKey(K key);
V getByKey(K key) throws Exception;

/**
* Returns all the values available in the data source or null if the operation does not make
* sense in the context of the data source.
*
* @return A collection of values or null if the operation is not implemented by this data source.
*/
Collection<V> getAll();
Collection<V> getAll() throws Exception;
}
Loading

0 comments on commit 583b1cc

Please sign in to comment.