Skip to content

Commit

Permalink
advanced unmodifiable support for change aware containers
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed May 5, 2024
1 parent 68ed0dd commit a6b259d
Show file tree
Hide file tree
Showing 11 changed files with 826 additions and 603 deletions.
54 changes: 29 additions & 25 deletions observable/pom.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.m-m-m</groupId>
<artifactId>mmm-value-parent</artifactId>
<version>${revision}</version>
</parent>
<artifactId>mmm-value-observable</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Java module providing observable values.</description>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mmm-value</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mmm-event</artifactId>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.m-m-m</groupId>
<artifactId>mmm-value-parent</artifactId>
<version>${revision}</version>
</parent>
<artifactId>mmm-value-observable</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Java module providing observable values.</description>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mmm-value</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mmm-event</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mmm-base</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,79 +1,94 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.value.observable.container.list;

import java.util.List;

import io.github.mmm.value.observable.container.list.impl.ChangeAwareListImpl;
import io.github.mmm.value.observable.container.list.impl.EmptyChangeAwareList;
import io.github.mmm.value.observable.container.list.impl.ImmutableChangeAwareList;

/**
* Factory for {@link ChangeAwareList}.
*
* @since 1.0.0
*/
public final class ChangeAwareLists {

private ChangeAwareLists() {

super();
}

/**
* @param <E> the type of the elements.
* @return an empty, immutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> empty() {

return EmptyChangeAwareList.INSTANCE;
}

/**
* @param <E> the type of the elements.
* @return a new empty mutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> of() {

return new ChangeAwareListImpl<>();
}

/**
* @param <E> the type of the elements.
* @param capacity the initial capacity of the list.
* @return a new empty mutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> of(int capacity) {

return new ChangeAwareListImpl<>(capacity);
}

/**
* @param <E> the type of the elements.
* @param list the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Please avoid to modify
* this {@link List} afterwards, as this will not trigger modification events.
* @return a new empty mutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> of(List<E> list) {

if (list instanceof ChangeAwareList) {
return (ChangeAwareList<E>) list;
}
return new ChangeAwareListImpl<>(list);
}

/**
* @param <E> the type of the elements.
* @param list the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Please avoid to modify
* this {@link List} afterwards, as this will not trigger modification events.
* @return a new empty mutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> ofUnmodifiable(List<E> list) {

if (list instanceof ImmutableChangeAwareList) {
return (ChangeAwareList<E>) list;
}
return new ImmutableChangeAwareList<>(list);
}

}
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.value.observable.container.list;

import java.util.List;
import java.util.function.Supplier;

import io.github.mmm.value.observable.container.list.impl.ChangeAwareListImpl;
import io.github.mmm.value.observable.container.list.impl.EmptyChangeAwareList;
import io.github.mmm.value.observable.container.list.impl.ImmutableChangeAwareList;
import io.github.mmm.value.observable.container.list.impl.ImmutableChangeAwareListView;

/**
* Factory for {@link ChangeAwareList}.
*
* @since 1.0.0
*/
public final class ChangeAwareLists {

private ChangeAwareLists() {

super();
}

/**
* @param <E> the type of the elements.
* @return an empty, immutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> empty() {

return EmptyChangeAwareList.INSTANCE;
}

/**
* @param <E> the type of the elements.
* @return a new empty mutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> of() {

return new ChangeAwareListImpl<>();
}

/**
* @param <E> the type of the elements.
* @param capacity the initial capacity of the list.
* @return a new empty mutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> of(int capacity) {

return new ChangeAwareListImpl<>(capacity);
}

/**
* @param <E> the type of the elements.
* @param list the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Please avoid to modify
* this {@link List} afterwards, as this will not trigger modification events.
* @return a new empty mutable {@link ChangeAwareList}.
*/
public static <E> ChangeAwareList<E> of(List<E> list) {

if (list instanceof ChangeAwareList) {
return (ChangeAwareList<E>) list;
}
return new ChangeAwareListImpl<>(list);
}

/**
* @param <E> the type of the elements.
* @param list the existing {@link List} implementation to wrap as {@link ChangeAwareList}. Modifying such
* {@link List} afterwards will not trigger modification events in the read-only view returned by this method.
* @return a unmodifiable {@link ChangeAwareList} that acts as a view on the given {@link List}.
*/
public static <E> ChangeAwareList<E> ofUnmodifiable(List<E> list) {

if (list instanceof ImmutableChangeAwareList<E> result) {
return result;
}
return new ImmutableChangeAwareList<>(list);
}

/**
* @param <E> the type of the elements.
* @param listSupplier the {@link Supplier} to the (mutable) {@link List} implementation to wrap as
* {@link ChangeAwareList}. Modifying such {@link List} afterwards will not trigger modification events in the
* read-only view returned by this method.
* @return a unmodifiable {@link ChangeAwareList} that acts as a view on the {@link Supplier#get() supplied}
* {@link List}.
*/
public static <E> ChangeAwareList<E> ofUnmodifiable(Supplier<List<E>> listSupplier) {

return new ImmutableChangeAwareListView<>(listSupplier);
}

}
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.value.observable.container.list.impl;

import java.util.Collection;
import java.util.List;

/**
* Immutable implementation of {@link io.github.mmm.value.observable.container.list.ChangeAwareList}.
*
* @param <E> the type of the elements in the container.
* @since 1.0.0
*/
public class ImmutableChangeAwareList<E> extends ReadOnlyChangeAwareList<E> {

private final List<E> list;

/**
* The constructor.
*
* @param list the internal {@link List} to adopt.
*/
public ImmutableChangeAwareList(List<E> list) {

super();
this.list = list;
}

@Override
public E get(int index) {

return this.list.get(index);
}

@Override
public int size() {

return this.list.size();
}

@Override
public int indexOf(Object element) {

return this.list.indexOf(element);
}

@Override
public int lastIndexOf(Object element) {

return this.list.lastIndexOf(element);
}

@Override
public boolean contains(Object element) {

return this.list.contains(element);
}

@Override
public boolean containsAll(Collection<?> collection) {

return this.list.containsAll(collection);
}

}
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.value.observable.container.list.impl;

import java.util.Collection;
import java.util.List;

/**
* Immutable implementation of {@link io.github.mmm.value.observable.container.list.ChangeAwareList}.
*
* @param <E> the type of the elements in the container.
* @since 1.0.0
*/
public class ImmutableChangeAwareList<E> extends ReadOnlyChangeAwareList<E> {

private final List<E> list;

/**
* The constructor.
*
* @param list the internal {@link List} to adopt.
*/
public ImmutableChangeAwareList(List<E> list) {

super();
this.list = list;
}

@Override
public E get(int index) {

return this.list.get(index);
}

@Override
public int size() {

return this.list.size();
}

@Override
public int indexOf(Object element) {

return this.list.indexOf(element);
}

@Override
public int lastIndexOf(Object element) {

return this.list.lastIndexOf(element);
}

@Override
public boolean contains(Object element) {

return this.list.contains(element);
}

@Override
public boolean containsAll(Collection<?> collection) {

return this.list.containsAll(collection);
}

}
Loading

0 comments on commit a6b259d

Please sign in to comment.