com.spotify.mobius.Next类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(85)

本文整理了Java中com.spotify.mobius.Next类的一些代码示例,展示了Next类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Next类的具体详情如下:
包路径:com.spotify.mobius.Next
类名称:Next

Next介绍

[英]This class represents the result of calling an Update function.

Upon calling an Update function with an Event and Model, a Next object will be returned that contains the new Model (if there is one) and Effect objects that describe which side-effects should take place.
[中]此类表示调用更新函数的结果。
调用带有事件和模型的更新函数后,将返回下一个对象,其中包含新模型(如果有)和描述应发生哪些副作用的效果对象。

代码示例

代码示例来源:origin: spotify/mobius

@Override
public void afterUpdate(M model, E event, Next<M, F> result) {
 if (result.hasModel()) {
  LOGGER.debug(LOGGING_PREFIX + "Model updated: {}", loggingTag, result.modelUnsafe());
 }
 for (F effect : result.effects()) {
  LOGGER.debug(LOGGING_PREFIX + "Effect dispatched: {}", loggingTag, effect);
 }
}

代码示例来源:origin: spotify/mobius

@Nonnull
 @Override
 public Next<M, Object> update(M model, E event) {
  return Next.next(update.apply(model, event));
 }
},

代码示例来源:origin: spotify/mobius

@Test
public void canMergeInnerEffects() throws Exception {
 Next<String, String> outerNext = Next.next("m", effects("f1", "f2"));
 Next<?, String> innerNext = dispatch(effects("f2", "f3"));
 Next<String, String> merged =
   Next.next(
     outerNext.modelOrElse("fail"), unionSets(innerNext.effects(), outerNext.effects()));
 assertEquals(Next.next("m", effects("f1", "f2", "f3")), merged);
}

代码示例来源:origin: spotify/mobius

@Nonnull
 @Override
 public Next<M, F> handleInnerEffects(M model, boolean modelUpdated, Set<FI> innerEffects) {
  return modelUpdated ? Next.<M, F>next(model) : Next.<M, F>noChange();
 }
};

代码示例来源:origin: spotify/mobius

/**
 * Get the model of this Next. This version is unsafe - if this next doesn't have a model, calling
 * this method will cause an exception to be thrown.
 *
 * <p>In almost all cases you should use {@link #modelOrElse} or {@link #ifHasModel} instead.
 *
 * @throws NoSuchElementException if this Next has no model
 */
@Nonnull
public M modelUnsafe() {
 if (!hasModel()) {
  throw new NoSuchElementException("there is no model in this Next<>");
 }
 // we know model is never null here since we just checked it.
 //noinspection ConstantConditions
 return model();
}

代码示例来源:origin: spotify/mobius

/**
 * Try to get the model from this Next, with a fallback if there isn't one.
 *
 * @param fallbackModel the default model to use if the Next doesn't have a model
 */
@Nonnull
public M modelOrElse(M fallbackModel) {
 checkNotNull(fallbackModel);
 if (hasModel()) {
  return modelUnsafe();
 } else {
  return fallbackModel;
 }
}

代码示例来源:origin: spotify/mobius

@Test
public void nextNoEffectsOnlyHasModel() throws Exception {
 Next<String, String> next = Next.next("foo");
 assertTrue(next.hasModel());
 assertFalse(next.hasEffects());
}

代码示例来源:origin: spotify/mobius

@Nonnull
 @Override
 public Next<M, F> handleInnerEffects(M model, boolean modelUpdated, Set<FI> innerEffects) {
  if (innerEffects.isEmpty()) {
   return modelUpdated ? Next.<M, F>next(model) : Next.<M, F>noChange();
  }
  Set<F> effects = new HashSet<>();
  for (FI innerEffect : innerEffects) {
   F outerEffect = checkNotNull(f).apply(innerEffect);
   effects.add(checkNotNull(outerEffect));
  }
  if (modelUpdated) {
   return Next.next(model, effects);
  } else {
   return Next.dispatch(effects);
  }
 }
};

代码示例来源:origin: spotify/mobius

@Test
public void canMergeInnerEffectsAndModel() throws Exception {
 Set<String> effects = setOf("f1", "f2");
 Next<Integer, String> innerNext = Next.next(1, effects("f2", "f3"));
 Next<String, String> merged =
   Next.next("m" + innerNext.modelOrElse(0), unionSets(effects, innerNext.effects()));
 assertEquals(Next.next("m1", effects("f1", "f2", "f3")), merged);
}

代码示例来源:origin: spotify/mobius

@Test
public void testEquals() throws Exception {
 Next<String, String> m1 = new AutoValue_Next<>("hi", ImmutableUtil.<String>emptySet());
 Next<String, String> m2 = Next.next("hi");
 Next<String, String> m3 = Next.next("hi", ImmutableUtil.<String>emptySet());
 Next<String, String> n2 = Next.next("hi", effects("a", "b"));
 Next<String, String> n3 = Next.next("hi", effects("b", "a"));
 Next<String, String> n4 = Next.next("hi", ImmutableUtil.setOf("b", "a"));
 Next<String, String> o2 = Next.next("hi", effects("a", "c", "b"));
 Next<String, String> o3 = Next.next("hi", effects("b", "a", "c"));
 Next<String, String> o4 = Next.next("hi", ImmutableUtil.setOf("c", "b", "a"));
 Next<String, String> p2 = Next.dispatch(effects("a", "c", "b"));
 Next<String, String> p3 = Next.dispatch(effects("b", "a", "c"));
 Next<String, String> p4 = Next.dispatch(ImmutableUtil.setOf("c", "b", "a"));
 Next<String, String> q2 = Next.next("hey");
 Next<String, String> q3 = Next.next("hey", Collections.<String>emptySet());
 Next<String, String> r2 = Next.next("hey", effects("a", "b"));
 Next<String, String> s2 = Next.next("hey", effects("a", "b", "c"));

代码示例来源:origin: spotify/mobius

@Test
public void nextNoopHasNoModelAndNoEffects() throws Exception {
 Next<String, String> next = noChange();
 assertFalse(next.hasModel());
 assertFalse(next.hasEffects());
}

代码示例来源:origin: spotify/mobius

@Test
public void shouldNotBeSensitiveToExternalMutation() throws Exception {
 Set<String> inputs = new HashSet<>();
 inputs.add("in");
 Next<String, String> next = Next.next("#", inputs);
 inputs.add("don't want to see this one");
 assertThat(next.effects(), equalTo((Set) Sets.newHashSet("in")));
}

代码示例来源:origin: spotify/mobius

@Test
public void nextEffectsOnlyHasEffects() throws Exception {
 Next<String, String> next = dispatch(effects("foo"));
 assertFalse(next.hasModel());
 assertTrue(next.hasEffects());
}

代码示例来源:origin: spotify/mobius

@SuppressWarnings("unused")
private Next<?, Number> canInferFromVarargOnlyEffects() {
 return dispatch(effects((short) 1, 2, (long) 3));
}

代码示例来源:origin: spotify/mobius

synchronized void update(E event) {
 if (!initialised) {
  eventsReceivedBeforeInit.add(event);
  return;
 }
 Next<M, F> next = store.update(event);
 next.ifHasModel(
   new Consumer<M>() {
    @Override
    public void accept(M model) {
     dispatchModel(model);
    }
   });
 dispatchEffects(next.effects());
}

代码示例来源:origin: spotify/mobius

@Override
protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {
 if (!item.hasEffects()) {
  mismatchDescription.appendText("no effects");
  return false;
 } else if (!matcher.matches(item.effects())) {
  mismatchDescription.appendText("bad effects: ");
  matcher.describeMismatch(item.effects(), mismatchDescription);
  return false;
 } else {
  mismatchDescription.appendText("has effects: ");
  matcher.describeMismatch(item.effects(), mismatchDescription);
  return true;
 }
}

代码示例来源:origin: spotify/mobius

@Test
public void testHasModelSpecificButMissing() throws Exception {
 next = noChange();
 matcher = hasModel(equalTo("a"));
 assertFalse(matcher.matches(next));
 matcher.describeMismatch(next, desc);
 assertEquals("no model", desc.toString());
}

代码示例来源:origin: spotify/mobius

@Override
protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {
 if (item.hasModel()) {
  mismatchDescription.appendText("has a model");
  return false;
 } else {
  mismatchDescription.appendText("no model");
  return true;
 }
}

代码示例来源:origin: com.spotify.mobius/mobius-core

/** Check if this Next contains effects. */
public final boolean hasEffects() {
 return !effects().isEmpty();
}

代码示例来源:origin: spotify/mobius

@Nonnull
 synchronized Next<M, F> update(E event) {
  Next<M, F> next = update.update(currentModel, checkNotNull(event));
  currentModel = next.modelOrElse(currentModel);
  return next;
 }
}

相关文章