diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/AbstractRedisCollection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/AbstractRedisCollection.java index 5c5f12cc4..9ca9d90a9 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/AbstractRedisCollection.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/AbstractRedisCollection.java @@ -1,83 +1,52 @@ +/* + * Copyright 2010 the original author or authors. + * + * 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 org.springframework.datastore.redis.util; +import java.util.AbstractCollection; import java.util.Collection; -import java.util.Iterator; -import org.springframework.datastore.redis.core.RedisTemplate; +import org.springframework.datastore.redis.connection.RedisCommands; -public abstract class AbstractRedisCollection implements RedisCollection { +/** + * Base implementation for Redis collections. + * + * @author Costin Leau + */ +public abstract class AbstractRedisCollection extends AbstractCollection implements RedisCollection { - protected RedisTemplate redisTemplate; - protected String redisKey; + protected final String key; + protected final RedisCommands commands; - public AbstractRedisCollection(RedisTemplate redisTemplate, String redisKey) { - this.redisTemplate = redisTemplate; - this.redisKey = redisKey; - } + public AbstractRedisCollection(String key, RedisCommands commands) { + this.key = key; + this.commands = commands; + } + @Override + public String getKey() { + return key; + } - /** - * They key used by the collection - * - * @return The redis key - */ - public String getRedisKey() { - return redisKey; - } + public abstract boolean add(String e); - public void clear() { - // redisTemplate.deleteKeys(redisKey); - } + public abstract void clear(); - public boolean isEmpty() { - return size() == 0; - } + public abstract boolean removeAll(Collection c); - public Object[] toArray() { - return new Object[0]; - } - - public boolean containsAll(Collection c) { - for (Object o : c) { - if(!contains(o)) return false; - } - return true; - } - - public boolean addAll(Collection c) { - boolean changed = false; - for (Object e : c) { - boolean elChange = add(e); - if(elChange && !changed) changed = true; - } - return changed; - } - - public boolean retainAll(Collection c) { - Iterator i = iterator(); - boolean changed = false; - while (i.hasNext()) { - Object o = i.next(); - if(!c.contains(o)) { - i.remove(); - changed = true; - } - } - return changed; - } - - public boolean removeAll(Collection c) { - boolean changed = false; - for (Object e : c) { - boolean elChange = remove(e); - if(elChange && !changed) changed = true; - } - return changed; - - } - - public Object[] toArray(Object[] array) { - return new Object[0]; - } - -} + public boolean retainAll(Collection c) { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisList.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisList.java new file mode 100644 index 000000000..45105d6f8 --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/DefaultRedisList.java @@ -0,0 +1,147 @@ +/* + * Copyright 2010 the original author or authors. + * + * 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 org.springframework.datastore.redis.util; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import org.springframework.datastore.redis.connection.RedisCommands; + +/** + * Default implementation for {@link RedisList}. + * + * @author Costin Leau + */ +public class DefaultRedisList extends AbstractRedisCollection implements RedisList { + + public DefaultRedisList(String key, RedisCommands commands) { + super(key, commands); + } + + @Override + public List range(int start, int end) { + return commands.lRange(key, start, end); + } + + @Override + public RedisList trim(int start, int end) { + commands.lTrim(key, start, end); + return this; + } + + private List content() { + return commands.lRange(key, 0, -1); + } + + @Override + public Iterator iterator() { + return content().iterator(); + } + + @Override + public int size() { + return commands.lLen(key); + } + + + @Override + public boolean add(String value) { + commands.rPush(key, value); + return true; + } + + @Override + public void clear() { + commands.lTrim(key, 0, -1); + } + + @Override + public boolean removeAll(Collection c) { + boolean modified = false; + for (Object object : c) { + Integer result = commands.lRem(key, 0, object.toString()); + modified |= (result != null && result.intValue() > 0); + } + + return modified; + } + + @Override + public void add(int index, String element) { + if (index == 0) { + commands.lPush(key, element); + } + else if (index == size()) { + commands.rPush(key, element); + } + + throw new IllegalArgumentException("Redis supports insertion only at the beginning or the end of the list"); + } + + @Override + public boolean addAll(int index, Collection c) { + for (String string : c) { + add(index, string); + } + + return true; + } + + @Override + public String get(int index) { + return commands.lIndex(key, index); + } + + @Override + public int indexOf(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public int lastIndexOf(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public ListIterator listIterator() { + throw new UnsupportedOperationException(); + } + + @Override + public ListIterator listIterator(int index) { + throw new UnsupportedOperationException(); + } + + @Override + public String remove(int index) { + throw new UnsupportedOperationException(); + } + + + @Override + public String set(int index, String element) { + String object = get(index); + commands.lSet(key, index, element); + return object; + } + + @Override + public List subList(int fromIndex, int toIndex) { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisCollection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisCollection.java index b7d29da8b..68ef25383 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisCollection.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisCollection.java @@ -1,21 +1,33 @@ +/* + * Copyright 2006-2009 the original author or authors. + * + * 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 org.springframework.datastore.redis.util; import java.util.Collection; -import java.util.Set; /** + * Basic interface for Redis collections. * - * @author Graeme Rocher - * + * @author Costin Leau */ -public interface RedisCollection extends Collection { +public interface RedisCollection extends Collection { - /** - * They key used by the collection - * - * @return The redis key - */ - String getRedisKey(); - - Set members(); + /** + * Returns the key used by the backing Redis store for this collection. + * + * @return Redis key + */ + String getKey(); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisList.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisList.java new file mode 100644 index 000000000..56005648b --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/util/RedisList.java @@ -0,0 +1,31 @@ +/* + * Copyright 2006-2009 the original author or authors. + * + * 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 org.springframework.datastore.redis.util; + +import java.util.List; + +/** + * Redis extension for {@link List} contract. Supports List specific + * operations backed by Redis commands. + * + * @author Costin Leau + */ +public interface RedisList extends RedisCollection, List { + + List range(int start, int end); + + RedisList trim(int start, int end); +}