<commit_before>/*
 * Copyright 2016-present The Material Motion Authors. All Rights Reserved.
 *
 * 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.google.android.reactive.motion.sources;

import com.google.android.indefinite.observable.IndefiniteObservable.Subscription;
import com.google.android.material.motion.physics.Integrator;
import com.google.android.material.motion.physics.forces.Spring;
import com.google.android.material.motion.physics.integrators.Rk4Integrator;
import com.google.android.material.motion.physics.math.Vector;
import com.google.android.reactive.motion.MotionObservable;
import com.google.android.reactive.motion.MotionObservable.MotionObserver;
import com.google.android.reactive.motion.MotionObservable.SimpleMotionObserver;
import com.google.android.reactive.motion.interactions.MaterialSpring;

/**
 * A source for physics springs.
 */
public final class PhysicsSpringSource<T> extends SpringSource<T> {

  public static final System SYSTEM = new System() {
    @Override
    public <T> SpringSource<T> create(MaterialSpring<?, T> spring) {
      return new PhysicsSpringSource<>(spring);
    }
  };

  private final MaterialSpring<?, T> spring;

  private Integrator integrator;
  private Spring springForce;

  private Subscription destinationSubscription;
  private Subscription frictionSubscription;
  private Subscription tensionSubscription;

  public PhysicsSpringSource(MaterialSpring<?, T> spring) {
    super(spring);
    this.spring = spring;
  }

  @Override
  protected void onConnect(final MotionObserver<T> observer) {
    integrator = new Rk4Integrator();
    springForce = new Spring();
    integrator.addForce(springForce);

    integrator.addListener(new Integrator.SimpleListener() {
      @Override
      public void onStart() {
        observer.state(MotionObservable.ACTIVE);
      }

      @Override
      public void onUpdate(Vector x, Vector v) {
        T value = spring.vectorizer.compose(x.getValues());
        observer.next(value);
      }

      @Override
      public void onStop() {
        observer.state(MotionObservable.AT_REST);
      }
    });
  }

  @Override
  protected void onEnable(MotionObserver<T> observer) {
    tensionSubscription = spring.tension.subscribe(new SimpleMotionObserver<Float>() {
      @Override
      public void next(Float value) {
        springForce.k = Spring.tensionFromOrigamiValue(value);<commit_msg>Remove per-target state from PhysicsSpringSource.<commit_after>/*
 * Copyright 2016-present The Material Motion Authors. All Rights Reserved.
 *
 * 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.google.android.reactive.motion.sources;

import com.google.android.indefinite.observable.IndefiniteObservable.Subscription;
import com.google.android.material.motion.physics.Integrator;
import com.google.android.material.motion.physics.Integrator.SimpleListener;
import com.google.android.material.motion.physics.forces.Spring;
import com.google.android.material.motion.physics.integrators.Rk4Integrator;
import com.google.android.material.motion.physics.math.Vector;
import com.google.android.reactive.motion.MotionObservable;
import com.google.android.reactive.motion.MotionObservable.MotionObserver;
import com.google.android.reactive.motion.MotionObservable.SimpleMotionObserver;
import com.google.android.reactive.motion.interactions.MaterialSpring;

import java.util.ArrayList;
import java.util.List;

/**
 * A source for physics springs.
 */
public final class PhysicsSpringSource<T> extends SpringSource<T> {

  public static final System SYSTEM = new System() {
    @Override
    public <T> SpringSource<T> create(MaterialSpring<?, T> spring) {
      return new PhysicsSpringSource<>(spring);
    }
  };

  private final MaterialSpring<?, T> spring;

  private final Integrator integrator;
  private final Spring springForce;
  private final List<Integrator.Listener> integratorListeners = new ArrayList<>();

  private Subscription destinationSubscription;
  private Subscription frictionSubscription;
  private Subscription tensionSubscription;

  public PhysicsSpringSource(MaterialSpring<?, T> spring) {
    super(spring);
    this.spring = spring;
    integrator = new Rk4Integrator();
    springForce = new Spring();
    integrator.addForce(springForce);
    integrator.addListener(new SimpleListener() {
      @Override
      public void onStart() {
        for (int i = 0, count = integratorListeners.size(); i < count; i++) {
          integratorListeners.get(i).onStart();
        }
      }

      @Override
      public void onUpdate(Vector x, Vector v) {
        for (int i = 0, count = integratorListeners.size(); i < count; i++) {
          integratorListeners.get(i).onUpdate(x, v);
        }
      }

      @Override
      public void onStop() {
        for (int i = 0, count = integratorListeners.size(); i < count; i++) {
          integratorListeners.get(i).onStop();
        }
      }
    });
  }

  @Override
  protected void onConnect(final MotionObserver<T> observer) {
    integratorListeners.add(new SimpleListener() {

      @Override
      public void onStart() {
        observer.state(MotionObservable.ACTIVE);
      }

      @Override
      public void onUpdate(Vector x, Vector v) {
        T value = spring.vectorizer.compose(x.getValues());
        observer.next(value);
      }

      @Override
      public void onStop() {
        observer.state(MotionObservable.AT_REST);
      }
    });
  }

  @Override
  protected void onEnable(MotionObserver<T> observer) {
    tensionSubscription = spring.tension.subscribe(new SimpleMotionObserver<Float>() {
      @Override
      public void next(Float value) {
        springForce.k = Spring.tensionFromOrigamiValue(value);
{'fraction_non_alphanumeric': 0.09703703703703703, 'fraction_numerical': 0.0034074074074074076, 'mean_word_length': 3.8049822064056937, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4559963', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1806, 'n_words': 493}