All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.torchmind.observable.ReadOnlyObservable Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 Johannes Donath 
 * and other copyright owners as documented in the project's IP log.
 *
 * 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.torchmind.observable;

import com.torchmind.observable.listener.ChangeListener;
import java.util.Optional;
import java.util.function.Supplier;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
 * Represents a property of a certain complex (or possibly wrapped) type which is capable of
 * inheriting or share its value with another property as well as notifying third parties of changes
 * to this property.
 *
 * @author Johannes Donath
 */
public interface ReadOnlyObservable {

  /**
   * Converts this observable into a standard Java supplier.
   */
  @NonNull
  default Supplier asSupplier() {
    return this::get;
  }

  /**
   * Returns the value exposed by this observable.
   *
   * @throws IllegalStateException when the state of this observable does not permit the retrieval
   * of its value.
   */
  V get();

  /**
   * 

Returns the value exposed by this observable wrapped in an optional.

* *

Note that null values will logically return an empty optional rather than returning an * optional which contains null.

* * @throws IllegalStateException when the state of this observable does not permit the retrieval * of its value. */ @NonNull default Optional getAsOptional() { return Optional.ofNullable(this.get()); } /** * Returns the passed default value if the property's value evaluates to null. * * @throws IllegalStateException when the state of this observable does not permit the retrieval * of its value. */ default V getOrDefault(V defaultValue) { V value = this.get(); if (value == null) { return defaultValue; } return value; } /** *

Registers a new listener with this observable which is invoked whenever the value exposed * through this observable changes.

* *

When the passed listener is already registered with this observable at the time of the * method call, the call will be ignored and cause no modification of the observable state.

*/ void registerListener(@NonNull ChangeListener listener); /** *

Removes a previously registered from this observable and thus prevents it from receiving * future updates from this observable when its exposed value changes.

* *

When the passed listener is not yet registered with this observable, the call will be * ignored and cause no modification to the observable state.

*/ void removeListener(@NonNull ChangeListener listener); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy