/** Recursively populates a list of contexts. */ private static void build(List<Context> list, Context context) { if (context == EMPTY_CONTEXT || list.contains(context)) { return; } if (context instanceof ChainContext) { ChainContext chainContext = (ChainContext) context; for (Context child : chainContext.contexts) { build(list, child); } } else { list.add(context); } }
/** Recursively populates a list of contexts. */ private static void build(List<Context> list, Context context) { if (context == EMPTY_CONTEXT || list.contains(context)) { return; } if (context instanceof ChainContext) { ChainContext chainContext = (ChainContext) context; for (Context child : chainContext.contexts) { build(list, child); } } else { list.add(context); } }
private static Context chain(Iterable<? extends Context> contexts) { // Flatten any chain contexts in the list, and remove duplicates final List<Context> list = new ArrayList<>(); for (Context context : contexts) { build(list, context); } switch (list.size()) { case 0: return empty(); case 1: return list.get(0); default: return new ChainContext(ImmutableList.copyOf(list)); } }
private static Context chain(Iterable<? extends Context> contexts) { // Flatten any chain contexts in the list, and remove duplicates final List<Context> list = new ArrayList<>(); for (Context context : contexts) { build(list, context); } switch (list.size()) { case 0: return empty(); case 1: return list.get(0); default: return new ChainContext(ImmutableList.copyOf(list)); } }