Example usage for java.util AbstractSet AbstractSet

List of usage examples for java.util AbstractSet AbstractSet

Introduction

In this page you can find the example usage for java.util AbstractSet AbstractSet.

Prototype

protected AbstractSet() 

Source Link

Document

Sole constructor.

Usage

From source file:org.sakaiproject.content.tool.ListItem.java

/**
 * Provides a huge wrapper around ResourceProperties to use it as a Map
 *
 * @param resourceProperties ResourceProperties to wrap
 * @return the wrapped value/*from  w w w . ja va  2s .c  o m*/
 */
private Map<String, Object> wrapResourcePropertiesInMap(final ResourceProperties resourceProperties) {
    return new AbstractMap<String, Object>() {
        public boolean isEmpty() {
            return !resourceProperties.getPropertyNames().hasNext();
        }

        public boolean containsKey(Object key) {
            return resourceProperties.get((String) key) != null;
        }

        public Object get(Object key) {
            return resourceProperties.get((String) key);
        }

        @Override
        public Set<Entry<String, Object>> entrySet() {
            return new AbstractSet<Entry<String, Object>>() {
                @Override
                public Iterator<Entry<String, Object>> iterator() {
                    return new Iterator<Entry<String, Object>>() {
                        private final Iterator<String> propertiesNames = resourceProperties.getPropertyNames();

                        public boolean hasNext() {
                            return propertiesNames.hasNext();
                        }

                        public Entry<String, Object> next() {
                            return new Entry<String, Object>() {
                                private final String key = propertiesNames.next();

                                public String getKey() {
                                    return key;
                                }

                                public Object getValue() {
                                    return resourceProperties.get(key);
                                }

                                public Object setValue(Object value) {
                                    throw new UnsupportedOperationException();
                                }
                            };
                        }

                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }

                @Override
                public int size() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}

From source file:org.sakaiproject.content.tool.ListItem.java

/**
 * Provides a huge wrapper around parameterParser to use it as a Map
 *
 * @param params ParameterParser to wrap
 * @return the wrapped value//  w  w  w.  j a va2  s  .  co  m
 */
private Map<String, ?> wrapParametersInMap(final ParameterParser params) {
    return new AbstractMap<String, Object>() {
        public boolean isEmpty() {
            return !params.getNames().hasNext();
        }

        public boolean containsKey(Object key) {
            return params.get((String) key) != null;
        }

        public Object get(Object key) {
            String[] value = params.getStrings((String) key);
            if (value == null || value.length == 0)
                return null;
            else if (value.length > 1) {
                return value;
            } else
                return value[0];
        }

        @Override
        public Set<Entry<String, Object>> entrySet() {
            return new AbstractSet<Entry<String, Object>>() {
                @Override
                public Iterator<Entry<String, Object>> iterator() {
                    return new Iterator<Entry<String, Object>>() {
                        private final Iterator<String> parametersNames = params.getNames();

                        public boolean hasNext() {
                            return parametersNames.hasNext();
                        }

                        public Entry<String, Object> next() {
                            return new Entry<String, Object>() {
                                private final String key = parametersNames.next();

                                public String getKey() {
                                    return key;
                                }

                                public Object getValue() {
                                    String[] value = params.getStrings(key);
                                    if (value == null || value.length == 0)
                                        return null;
                                    else if (value.length > 1) {
                                        return value;
                                    } else
                                        return value[0];
                                }

                                public Object setValue(Object value) {
                                    throw new UnsupportedOperationException();
                                }
                            };
                        }

                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }

                @Override
                public int size() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}