method2testcases
stringlengths
118
6.63k
### Question: StringUtils { public static boolean isNoneBlank(final CharSequence... css) { return !isAnyBlank(css); } static boolean isNoneBlank(final CharSequence... css); static boolean isEmpty(final Object[] array); static boolean isBlank(final CharSequence cs); }### Answer: @Test public void testIsNoneBlank() { Assert.assertFalse(StringUtils.isNoneBlank((CharSequence) null)); Assert.assertFalse(StringUtils.isNoneBlank("")); Assert.assertFalse(StringUtils.isNoneBlank("", "", "")); Assert.assertTrue(StringUtils.isNoneBlank("a", "b", "c")); }
### Question: StringUtils { public static boolean isEmpty(final Object[] array) { return array == null || array.length == 0; } static boolean isNoneBlank(final CharSequence... css); static boolean isEmpty(final Object[] array); static boolean isBlank(final CharSequence cs); }### Answer: @Test public void testIsEmpty() { Assert.assertTrue(true); Assert.assertTrue(StringUtils.isEmpty(new String[0])); Assert.assertFalse(StringUtils.isEmpty(new String[]{"a", "b", "c"})); }
### Question: StringUtils { public static boolean isBlank(final CharSequence cs) { int strLen; if (cs == null || (strLen = cs.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; } static boolean isNoneBlank(final CharSequence... css); static boolean isEmpty(final Object[] array); static boolean isBlank(final CharSequence cs); }### Answer: @Test public void testIsBlank() { Assert.assertTrue(StringUtils.isBlank(null)); Assert.assertTrue(StringUtils.isBlank("")); Assert.assertTrue(StringUtils.isBlank(" ")); Assert.assertFalse(StringUtils.isBlank("foobar")); Assert.assertFalse(StringUtils.isBlank("foo bar")); }
### Question: EtcdConfigLoader implements ConfigLoader<EtcdConfig> { private void etcdLoad(final Supplier<Context> context, final LoaderHandler<EtcdConfig> handler, final EtcdConfig config) { if (config != null) { check(config); LOGGER.info("loader etcd config: {}", config); String fileExtension = config.getFileExtension(); PropertyLoader propertyLoader = LOADERS.get(fileExtension); if (propertyLoader == null) { throw new ConfigException("etcd.fileExtension setting error, The loader was not found"); } InputStream pull = client.pull(config); Optional.ofNullable(pull) .map(e -> propertyLoader.load("remote.etcd." + fileExtension, e)) .ifPresent(e -> context.get().getOriginal().load(() -> context.get().withSources(e), this::etcdFinish)); handler.finish(context, config); try { client.addListener(context, (c1, c2) -> this.passive(c1, null, c2), config); } catch (Exception e) { LOGGER.error("passive etcd remote started error...."); } } else { throw new ConfigException("etcd config is null"); } } EtcdConfigLoader(); EtcdConfigLoader(final EtcdClient client); @Override void load(final Supplier<Context> context, final LoaderHandler<EtcdConfig> handler); @Override void passive(final Supplier<Context> context, final PassiveHandler<Config> handler, final Config config); }### Answer: @Test public void testEtcdLoad() { ConfigScan.scan(); ServerConfigLoader loader = new ServerConfigLoader(); EtcdConfigLoader etcdConfigLoader = new EtcdConfigLoader(client); loader.load(ConfigLoader.Context::new, (context, config) -> { if (config != null) { if (StringUtils.isNoneBlank(config.getConfigMode())) { String configMode = config.getConfigMode(); if (configMode.equals("etcd")) { etcdConfigLoader.load(context, this::assertTest); } } } }); }
### Question: JavaBeanBinder extends BeanBinder { @Override public <T> T bind(final PropertyName name, final BindData<T> target, final Binder.Env env, final PropertyBinder propertyBinder) { boolean hasKnownBindableProperties = env.getSource().containsDescendantOf(name); Bean<T> bean = Bean.get(target, hasKnownBindableProperties); if (bean == null) { return null; } BeanSupplier<T> beanSupplier = bean.getSupplier(target); boolean bound = bind(propertyBinder, bean, beanSupplier); return bound ? beanSupplier.get() : null; } @Override T bind(final PropertyName name, final BindData<T> target, final Binder.Env env, final PropertyBinder propertyBinder); }### Answer: @Test public void testJavaBeanBind() { String name = "hmily.yml"; Map<String, Object> map = new HashMap<>(); map.put("hmily.stringTest", "123"); map.put("hmily.integerTest", 456); map.put("hmily.doubleTest", 42.12); map.put("hmily.longTest", 100L); map.put("hmily.chartTest", 'a'); map.put("hmily.floatTest", 12.1F); map.put("hmily.boolTest", true); PropertyKeySource<?> propertySource = new MapPropertyKeySource(name, map); ConfigPropertySource configPropertySource = new DefaultConfigPropertySource<>(propertySource, PropertyKeyParse.INSTANCE); Binder binder = Binder.of(configPropertySource); BindData<JavaBeanBinderTest> data = BindData.of(DataType.of(JavaBeanBinderTest.class), JavaBeanBinderTest::new); JavaBeanBinderTest bind = binder.bind("hmily", data); Assert.assertEquals(map.get("hmily.stringTest"), bind.getStringTest()); Assert.assertEquals(map.get("hmily.integerTest"), bind.getIntegerTest()); Assert.assertEquals(map.get("hmily.doubleTest"), bind.getDoubleTest()); Assert.assertEquals(map.get("hmily.chartTest"), bind.getChartTest()); Assert.assertEquals(map.get("hmily.longTest"), bind.getLongTest()); Assert.assertEquals(map.get("hmily.floatTest"), bind.getFloatTest()); Assert.assertEquals(map.get("hmily.boolTest"), bind.getBoolTest()); } @Test public void testJavaBeanBindParse() { String name = "hmily.yml"; Map<String, Object> map = new HashMap<>(); map.put("hmily.stringTest", 123); map.put("hmily.integerTest", "123"); map.put("hmily.doubleTest", "123"); map.put("hmily.longTest", "123"); map.put("hmily.chartTest", "A"); map.put("hmily.floatTest", "123"); map.put("hmily.boolTest", "true"); PropertyKeySource<?> propertySource = new MapPropertyKeySource(name, map); ConfigPropertySource configPropertySource = new DefaultConfigPropertySource<>(propertySource, PropertyKeyParse.INSTANCE); Binder binder = Binder.of(configPropertySource); BindData<JavaBeanBinderTest> data = BindData.of(DataType.of(JavaBeanBinderTest.class), JavaBeanBinderTest::new); JavaBeanBinderTest bind = binder.bind("hmily", data); Assert.assertEquals("123", bind.getStringTest()); Assert.assertEquals(Integer.valueOf(123), bind.getIntegerTest()); Assert.assertEquals(Double.valueOf(123), bind.getDoubleTest()); Assert.assertEquals(Character.valueOf('A'), bind.getChartTest()); Assert.assertEquals(Long.valueOf(123), bind.getLongTest()); Assert.assertEquals(Float.valueOf(123), bind.getFloatTest()); Assert.assertEquals(Boolean.TRUE, bind.getBoolTest()); }
### Question: MapBinder extends AggregateBinder<Map<Object, Object>> { @Override public Object bind(final PropertyName propertyName, final BindData<?> target, final Binder.Env env, final AggregateElementBinder elementBinder) { Map<Object, Object> map = new HashMap<>(16); boolean hasDescendants = env.getSource().containsDescendantOf(propertyName); if (!hasDescendants) { new EntryBinder(propertyName, target, env, elementBinder).bindEntries(env.getSource(), map); } return map.isEmpty() ? null : map; } MapBinder(final Binder.Env env); @Override Object bind(final PropertyName propertyName, final BindData<?> target, final Binder.Env env, final AggregateElementBinder elementBinder); @Override Object merge(final Supplier<?> targetValue, final Map<Object, Object> object); @Override boolean isAllowRecursiveBinding(final Binder.Env source); }### Answer: @Test public void testMap() { String name = "hmily.yml"; Map<String, Object> map = new HashMap<>(); map.put("hmily.map.userName", "sixh"); map.put("hmily.map.passWord", 123456); PropertyKeySource<?> propertySource = new MapPropertyKeySource(name, map); ConfigPropertySource configPropertySource = new DefaultConfigPropertySource<>(propertySource, PropertyKeyParse.INSTANCE); Binder binder = Binder.of(configPropertySource); BindData<MapPojo> data = BindData.of(DataType.of(MapPojo.class), MapPojo::new); MapPojo bind = binder.bind("hmily", data); Assert.assertEquals(bind.getMap().get("userName"), map.get("hmily.map.userName")); Assert.assertEquals(bind.getMap().get("passWord"), map.get("hmily.map.passWord")); System.out.println(bind); } @Test public void testMapGeneric() { String name = "hmily.yml"; Map<String, Object> map = new HashMap<>(); map.put("hmily.map2.userName", "456"); map.put("hmily.map2.passWord", 123456); PropertyKeySource<?> propertySource = new MapPropertyKeySource(name, map); ConfigPropertySource configPropertySource = new DefaultConfigPropertySource<>(propertySource, PropertyKeyParse.INSTANCE); Binder binder = Binder.of(configPropertySource); BindData<MapPojo> data = BindData.of(DataType.of(MapPojo.class), MapPojo::new); MapPojo bind = binder.bind("hmily", data); Assert.assertEquals(bind.getMap2().get("userName"), Integer.valueOf(456)); Assert.assertEquals(bind.getMap2().get("passWord"), map.get("hmily.map2.passWord")); System.out.println(bind); }
### Question: MathUtil { public static boolean isOdd(int number) { return number % 2 == 1; } static boolean isOdd(int number); }### Answer: @Test public void testOdd() { assertTrue(MathUtil.isOdd(3)); assertFalse(MathUtil.isOdd(4)); }
### Question: GenericWindowing implements Windowing { @Override public long getFirstContainingTime(long Tl, long Tr, long T) { assert 0 <= Tl && Tl <= Tr && Tr < T; long l = T-1 - Tr, r = T-1 - Tl, length = Tr - Tl + 1; if (!addWindowsUntilLength(length)) { return -1; } long firstMarker = firstWindowOfLength.ceilingEntry(length).getValue(); if (firstMarker >= l) { return firstMarker + Tr + 1; } else { addWindowsPastMarker(l); long currWindowL = windowStartMarkers.floor(l), currWindowR = windowStartMarkers.higher(l) - 1; if (r <= currWindowR) { return T; } else { assert currWindowR - currWindowL + 1 >= length; return currWindowR + Tr + 2; } } } GenericWindowing(WindowLengthsSequence windowLengths); @Override long getFirstContainingTime(long Tl, long Tr, long T); @Override long getSizeOfFirstWindow(); @Override List<Long> getWindowsCoveringUpto(long N); }### Answer: @Test public void getFirstContainingTime() throws Exception { GenericWindowing exp = new GenericWindowing(new ExponentialWindowLengths(2)); assertEquals(101, exp.getFirstContainingTime(98, 99, 100)); assertEquals(103, exp.getFirstContainingTime(96, 99, 100)); assertEquals(107, exp.getFirstContainingTime(92, 99, 100)); assertEquals(115, exp.getFirstContainingTime(84, 99, 100)); }
### Question: GenericWindowing implements Windowing { @Override public long getSizeOfFirstWindow() { return firstWindowLength; } GenericWindowing(WindowLengthsSequence windowLengths); @Override long getFirstContainingTime(long Tl, long Tr, long T); @Override long getSizeOfFirstWindow(); @Override List<Long> getWindowsCoveringUpto(long N); }### Answer: @Test public void getSizeOfFirstWindow() throws Exception { GenericWindowing exp = new GenericWindowing(new ExponentialWindowLengths(2)); assertEquals(1, exp.getSizeOfFirstWindow(), 1); final long S = 2492; GenericWindowing rp = new GenericWindowing(new WindowLengthsSequence() { private long i = 1; @Override public long nextWindowLength() { return i++ * S; } }); assertEquals(S, rp.getSizeOfFirstWindow()); }
### Question: GenericWindowing implements Windowing { @Override public List<Long> getWindowsCoveringUpto(long N) { if (N <= 0) return Collections.emptyList(); addWindowsPastMarker(N); List<Long> ret = new ArrayList<>(); long prevMarker = 0; for (long currMarker: windowStartMarkers) { if (currMarker == 0) continue; if (currMarker <= N) { ret.add(currMarker - prevMarker); prevMarker = currMarker; } else { break; } } return ret; } GenericWindowing(WindowLengthsSequence windowLengths); @Override long getFirstContainingTime(long Tl, long Tr, long T); @Override long getSizeOfFirstWindow(); @Override List<Long> getWindowsCoveringUpto(long N); }### Answer: @Test public void getWindowsCoveringUpto() throws Exception { GenericWindowing exp = new GenericWindowing(new ExponentialWindowLengths(2)); assertThat(exp.getWindowsCoveringUpto(62), is(Arrays.asList(1L, 2L, 4L, 8L, 16L))); assertThat(exp.getWindowsCoveringUpto(63), is(Arrays.asList(1L, 2L, 4L, 8L, 16L, 32L))); }
### Question: WorldLoader { @Nullable public static World worldFromConfig(@NotNull ConfigurationSection conf, boolean useUUID) { if (useUUID) { UUID worldUUID; try { worldUUID = UUID.fromString(conf.getString(WORLD_UID, "")); } catch (IllegalArgumentException e) { return null; } return Bukkit.getWorld(worldUUID); } else { String worldName = conf.getString(WORLD_NAME); if (worldName == null) { return null; } return Bukkit.getWorld(worldName); } } @NotNull static ConfigurationSection locationToConfig(@NotNull Location location, boolean useUUID); @Nullable static Location locationFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); @NotNull static ConfigurationSection blockLocationToConfig(@NotNull Location location, boolean useUUID); @Nullable static Location blockLocationFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); @Nullable static Location worldlessBlockLocationFromConfig(@NotNull ConfigurationSection conf); @Nullable static World worldFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); static final String WORLD_UID; static final String WORLD_NAME; static final String X; static final String Y; static final String Z; static final String YAW; static final String PITCH; }### Answer: @Test public void worldFromConfValidName() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.WORLD_NAME, "test"); when(Bukkit.getWorld("test")).thenReturn(mock(World.class)); World w = WorldLoader.worldFromConfig(conf, false); Assert.assertNotNull(w); } @Test public void worldFromConfValidUUID() { YamlConfiguration conf = new YamlConfiguration(); UUID uuid = UUID.randomUUID(); conf.set(WorldLoader.WORLD_UID, uuid.toString()); when(Bukkit.getWorld(uuid)).thenReturn(mock(World.class)); World w = WorldLoader.worldFromConfig(conf, true); Assert.assertNotNull(w); } @Test public void worldFromConfInValidName() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.WORLD_NAME, "aa"); World w = WorldLoader.worldFromConfig(conf, true); Assert.assertNull(w); } @Test public void worldFromConfInValidUUID() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.WORLD_UID, UUID.randomUUID()); World w = WorldLoader.worldFromConfig(conf, true); Assert.assertNull(w); } @Test public void worldFromConfNullName() { World w = WorldLoader.worldFromConfig(new YamlConfiguration(), false); Assert.assertNull(w); } @Test public void worldFromConfNullUUID() { World w = WorldLoader.worldFromConfig(new YamlConfiguration(), true); Assert.assertNull(w); }
### Question: WorldLoader { @Nullable public static Location blockLocationFromConfig(@NotNull ConfigurationSection conf, boolean useUUID) { World world = worldFromConfig(conf, useUUID); if (world == null) { return null; } Location loc = worldlessBlockLocationFromConfig(conf); if (loc == null) { return null; } loc.setWorld(world); return loc; } @NotNull static ConfigurationSection locationToConfig(@NotNull Location location, boolean useUUID); @Nullable static Location locationFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); @NotNull static ConfigurationSection blockLocationToConfig(@NotNull Location location, boolean useUUID); @Nullable static Location blockLocationFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); @Nullable static Location worldlessBlockLocationFromConfig(@NotNull ConfigurationSection conf); @Nullable static World worldFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); static final String WORLD_UID; static final String WORLD_NAME; static final String X; static final String Y; static final String Z; static final String YAW; static final String PITCH; }### Answer: @Test public void blockLocationFromConfigValid() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.X, 6); conf.set(WorldLoader.Y, 0); conf.set(WorldLoader.Z, -4); conf.set(WorldLoader.WORLD_NAME, "test"); when(Bukkit.getWorld("test")).thenReturn(mock(World.class)); Location loc = WorldLoader.blockLocationFromConfig(conf, false); Assert.assertNotNull(loc); } @Test public void blockLocationFromConfigNoWorld() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.X, 6); conf.set(WorldLoader.Y, 0); conf.set(WorldLoader.Z, -4); Location loc = WorldLoader.blockLocationFromConfig(conf, false); Assert.assertNull(loc); } @Test public void blockLocationFromConfigInvalidPos() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.X, 6); conf.set(WorldLoader.Y, "ye"); conf.set(WorldLoader.Z, -4); conf.set(WorldLoader.WORLD_NAME, "test"); when(Bukkit.getWorld("test")).thenReturn(mock(World.class)); Location loc = WorldLoader.blockLocationFromConfig(conf, false); Assert.assertNull(loc); }
### Question: FileUtils { @NotNull public static File createDatafolderFile(@NotNull Plugin plugin, @NotNull String... children) throws IOException { File file = getDatafolderFile(plugin, children); createFileSafely(file); return file; } static boolean createParentFolders(@NotNull File file); @NotNull static File createDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); static void createFileSafely(@NotNull File file); static boolean createFolder(@NotNull Plugin plugin, @NotNull String... children); static boolean createFolderSafely(@NotNull File folder); static boolean writeFromInternal(Plugin plugin, String[] externalPath, String... internalPath); static boolean writeFromInternal(Plugin plugin, String[] externalPath, Class<?> resourceClass, String... internalPath); static boolean writeFromInternal(Plugin plugin, Class<?> resourceClass, String... path); static boolean writeFromInternal(Plugin plugin, String... path); static boolean writeFromInternal(File outFile, String... internalPath); static boolean writeFromInternal(File outFile, Class<?> resourceClass, String... internalPath); static boolean write(@NotNull String string, @NotNull Plugin plugin, @NotNull String... path); static boolean write(@NotNull String string, @NotNull File file); static boolean save(@NotNull InputStream in, @NotNull Plugin plugin, @NotNull String... path); static boolean save(@NotNull InputStream in, @NotNull File file); static String[] splitAtSlash(String path); @NotNull static String joinPath(String... path); @NotNull static String joinPath(boolean abs, String... path); @NotNull static String joinPath(char delimiter, boolean abs, String... path); static void extractInternalFolder(@NotNull Plugin plugin, @NotNull File file, @NotNull String... folderPath); @NotNull static File getDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); @Nullable static String read(@NotNull Plugin plugin, @NotNull String... path); @Nullable static String read(@NotNull File file); @Nullable static List<File> getFiles(@NotNull Plugin plugin, @NotNull String... subPath); @Nullable static List<File> getFiles(@NotNull File folder); @Nullable static List<String> getFileNames(@NotNull Plugin plugin, @NotNull String... subPath); @NotNull static List<String> getFileNames(@NotNull List<File> files); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull Plugin plugin, @NotNull String... children); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull File file); @Nullable static InputStream getInternalFileStream(@NotNull String... absPath); @Nullable static InputStream getInternalFileStream(@NotNull Class<?> resourceClass, @NotNull String... absPath); @Nullable static String readInternalFile(@NotNull String... absPath); @Nullable static String readInternalFile(@NotNull Class<?> resourceClass, @NotNull String... absPath); static Logger logger; }### Answer: @Test public void createDataFolderCreatesCorrectPath() throws IOException { File file = FileUtils.createDatafolderFile(plugin, "test.json"); assertTrue(file.exists()); assertTrue(file.isFile()); assertEquals(dataFolder.getRoot().getPath() + File.separator + "test.json", file.getPath()); } @Test public void createDataFolderCreatesCorrectSubPath() throws IOException { File file = FileUtils.createDatafolderFile(plugin, "testii", "test.json"); assertTrue(file.exists()); assertTrue(file.isFile()); String pathExpected = dataFolder.getRoot().getPath() + File.separator + "testii" + File.separator + "test.json"; assertEquals(pathExpected, file.getPath()); } @Test public void createDataFolderCreatesCorrectSubSubPath() throws IOException { File file = FileUtils.createDatafolderFile(plugin, "testii", "testii2", "test.json"); assertTrue(file.exists()); assertTrue(file.isFile()); String pathExpected = dataFolder.getRoot().getPath() + File.separator + "testii" + File.separator + "testii2" + File.separator + "test.json"; assertEquals(pathExpected, file.getPath()); }
### Question: FileUtils { public static boolean write(@NotNull String string, @NotNull Plugin plugin, @NotNull String... path) throws IOException { return write(string, createDatafolderFile(plugin, path)); } static boolean createParentFolders(@NotNull File file); @NotNull static File createDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); static void createFileSafely(@NotNull File file); static boolean createFolder(@NotNull Plugin plugin, @NotNull String... children); static boolean createFolderSafely(@NotNull File folder); static boolean writeFromInternal(Plugin plugin, String[] externalPath, String... internalPath); static boolean writeFromInternal(Plugin plugin, String[] externalPath, Class<?> resourceClass, String... internalPath); static boolean writeFromInternal(Plugin plugin, Class<?> resourceClass, String... path); static boolean writeFromInternal(Plugin plugin, String... path); static boolean writeFromInternal(File outFile, String... internalPath); static boolean writeFromInternal(File outFile, Class<?> resourceClass, String... internalPath); static boolean write(@NotNull String string, @NotNull Plugin plugin, @NotNull String... path); static boolean write(@NotNull String string, @NotNull File file); static boolean save(@NotNull InputStream in, @NotNull Plugin plugin, @NotNull String... path); static boolean save(@NotNull InputStream in, @NotNull File file); static String[] splitAtSlash(String path); @NotNull static String joinPath(String... path); @NotNull static String joinPath(boolean abs, String... path); @NotNull static String joinPath(char delimiter, boolean abs, String... path); static void extractInternalFolder(@NotNull Plugin plugin, @NotNull File file, @NotNull String... folderPath); @NotNull static File getDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); @Nullable static String read(@NotNull Plugin plugin, @NotNull String... path); @Nullable static String read(@NotNull File file); @Nullable static List<File> getFiles(@NotNull Plugin plugin, @NotNull String... subPath); @Nullable static List<File> getFiles(@NotNull File folder); @Nullable static List<String> getFileNames(@NotNull Plugin plugin, @NotNull String... subPath); @NotNull static List<String> getFileNames(@NotNull List<File> files); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull Plugin plugin, @NotNull String... children); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull File file); @Nullable static InputStream getInternalFileStream(@NotNull String... absPath); @Nullable static InputStream getInternalFileStream(@NotNull Class<?> resourceClass, @NotNull String... absPath); @Nullable static String readInternalFile(@NotNull String... absPath); @Nullable static String readInternalFile(@NotNull Class<?> resourceClass, @NotNull String... absPath); static Logger logger; }### Answer: @Test public void writeFailsWhenGivenFileIsFolder() throws IOException { assertFalse(FileUtils.write("whatever", plugin)); }
### Question: FileUtils { @NotNull public static File getDatafolderFile(@NotNull Plugin plugin, @NotNull String... children) { try { return Paths.get(plugin.getDataFolder().getAbsolutePath(), children).toFile(); } catch (NullPointerException e) { throw new IllegalArgumentException( "One of the children given is null. children: " + joinPath(File.separatorChar, false, children)); } } static boolean createParentFolders(@NotNull File file); @NotNull static File createDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); static void createFileSafely(@NotNull File file); static boolean createFolder(@NotNull Plugin plugin, @NotNull String... children); static boolean createFolderSafely(@NotNull File folder); static boolean writeFromInternal(Plugin plugin, String[] externalPath, String... internalPath); static boolean writeFromInternal(Plugin plugin, String[] externalPath, Class<?> resourceClass, String... internalPath); static boolean writeFromInternal(Plugin plugin, Class<?> resourceClass, String... path); static boolean writeFromInternal(Plugin plugin, String... path); static boolean writeFromInternal(File outFile, String... internalPath); static boolean writeFromInternal(File outFile, Class<?> resourceClass, String... internalPath); static boolean write(@NotNull String string, @NotNull Plugin plugin, @NotNull String... path); static boolean write(@NotNull String string, @NotNull File file); static boolean save(@NotNull InputStream in, @NotNull Plugin plugin, @NotNull String... path); static boolean save(@NotNull InputStream in, @NotNull File file); static String[] splitAtSlash(String path); @NotNull static String joinPath(String... path); @NotNull static String joinPath(boolean abs, String... path); @NotNull static String joinPath(char delimiter, boolean abs, String... path); static void extractInternalFolder(@NotNull Plugin plugin, @NotNull File file, @NotNull String... folderPath); @NotNull static File getDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); @Nullable static String read(@NotNull Plugin plugin, @NotNull String... path); @Nullable static String read(@NotNull File file); @Nullable static List<File> getFiles(@NotNull Plugin plugin, @NotNull String... subPath); @Nullable static List<File> getFiles(@NotNull File folder); @Nullable static List<String> getFileNames(@NotNull Plugin plugin, @NotNull String... subPath); @NotNull static List<String> getFileNames(@NotNull List<File> files); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull Plugin plugin, @NotNull String... children); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull File file); @Nullable static InputStream getInternalFileStream(@NotNull String... absPath); @Nullable static InputStream getInternalFileStream(@NotNull Class<?> resourceClass, @NotNull String... absPath); @Nullable static String readInternalFile(@NotNull String... absPath); @Nullable static String readInternalFile(@NotNull Class<?> resourceClass, @NotNull String... absPath); static Logger logger; }### Answer: @Test public void getDatafolderFileDoesNotCreateAnything() { File file = FileUtils.getDatafolderFile(plugin, "notafile.txt"); assertFalse(file.exists()); } @Test(expected = IllegalArgumentException.class) public void getDatafolderFileWithNullElementReturnsNull() { FileUtils.getDatafolderFile(plugin, null, "notafile.txt"); } @Test(expected = IllegalArgumentException.class) public void getDatafolderFileWithNullElementReturnsNull2() { FileUtils.getDatafolderFile(plugin, "folder", null); }
### Question: FileUtils { @Nullable public static List<File> getFiles(@NotNull Plugin plugin, @NotNull String... subPath) { return getFiles(getDatafolderFile(plugin, subPath)); } static boolean createParentFolders(@NotNull File file); @NotNull static File createDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); static void createFileSafely(@NotNull File file); static boolean createFolder(@NotNull Plugin plugin, @NotNull String... children); static boolean createFolderSafely(@NotNull File folder); static boolean writeFromInternal(Plugin plugin, String[] externalPath, String... internalPath); static boolean writeFromInternal(Plugin plugin, String[] externalPath, Class<?> resourceClass, String... internalPath); static boolean writeFromInternal(Plugin plugin, Class<?> resourceClass, String... path); static boolean writeFromInternal(Plugin plugin, String... path); static boolean writeFromInternal(File outFile, String... internalPath); static boolean writeFromInternal(File outFile, Class<?> resourceClass, String... internalPath); static boolean write(@NotNull String string, @NotNull Plugin plugin, @NotNull String... path); static boolean write(@NotNull String string, @NotNull File file); static boolean save(@NotNull InputStream in, @NotNull Plugin plugin, @NotNull String... path); static boolean save(@NotNull InputStream in, @NotNull File file); static String[] splitAtSlash(String path); @NotNull static String joinPath(String... path); @NotNull static String joinPath(boolean abs, String... path); @NotNull static String joinPath(char delimiter, boolean abs, String... path); static void extractInternalFolder(@NotNull Plugin plugin, @NotNull File file, @NotNull String... folderPath); @NotNull static File getDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); @Nullable static String read(@NotNull Plugin plugin, @NotNull String... path); @Nullable static String read(@NotNull File file); @Nullable static List<File> getFiles(@NotNull Plugin plugin, @NotNull String... subPath); @Nullable static List<File> getFiles(@NotNull File folder); @Nullable static List<String> getFileNames(@NotNull Plugin plugin, @NotNull String... subPath); @NotNull static List<String> getFileNames(@NotNull List<File> files); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull Plugin plugin, @NotNull String... children); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull File file); @Nullable static InputStream getInternalFileStream(@NotNull String... absPath); @Nullable static InputStream getInternalFileStream(@NotNull Class<?> resourceClass, @NotNull String... absPath); @Nullable static String readInternalFile(@NotNull String... absPath); @Nullable static String readInternalFile(@NotNull Class<?> resourceClass, @NotNull String... absPath); static Logger logger; }### Answer: @Test public void getFilesReturnAllFilesInFolder() throws IOException { Random random = new Random(); int nrOfFiles = random.nextInt(5) + 10; List<File> createdFiles = new ArrayList<>(); for (int i = 0; i < nrOfFiles; i++) { File file = new File(plugin.getDataFolder(), "file" + i); createdFiles.add(file); assertTrue(file.createNewFile()); } List<File> files = FileUtils.getFiles(plugin); assertNotNull(files); assertEquals(nrOfFiles, files.size()); Collections.sort(createdFiles); Collections.sort(files); assertEquals(createdFiles, files); } @Test public void getFilesInEmptyFolderReturnEmptyList() { List<File> files = FileUtils.getFiles(plugin); assertNotNull(files); assertTrue(files.isEmpty()); }
### Question: FileUtils { @Nullable public static List<String> getFileNames(@NotNull Plugin plugin, @NotNull String... subPath) { List<File> files = getFiles(plugin, subPath); if (files == null) { return null; } return getFileNames(files); } static boolean createParentFolders(@NotNull File file); @NotNull static File createDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); static void createFileSafely(@NotNull File file); static boolean createFolder(@NotNull Plugin plugin, @NotNull String... children); static boolean createFolderSafely(@NotNull File folder); static boolean writeFromInternal(Plugin plugin, String[] externalPath, String... internalPath); static boolean writeFromInternal(Plugin plugin, String[] externalPath, Class<?> resourceClass, String... internalPath); static boolean writeFromInternal(Plugin plugin, Class<?> resourceClass, String... path); static boolean writeFromInternal(Plugin plugin, String... path); static boolean writeFromInternal(File outFile, String... internalPath); static boolean writeFromInternal(File outFile, Class<?> resourceClass, String... internalPath); static boolean write(@NotNull String string, @NotNull Plugin plugin, @NotNull String... path); static boolean write(@NotNull String string, @NotNull File file); static boolean save(@NotNull InputStream in, @NotNull Plugin plugin, @NotNull String... path); static boolean save(@NotNull InputStream in, @NotNull File file); static String[] splitAtSlash(String path); @NotNull static String joinPath(String... path); @NotNull static String joinPath(boolean abs, String... path); @NotNull static String joinPath(char delimiter, boolean abs, String... path); static void extractInternalFolder(@NotNull Plugin plugin, @NotNull File file, @NotNull String... folderPath); @NotNull static File getDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); @Nullable static String read(@NotNull Plugin plugin, @NotNull String... path); @Nullable static String read(@NotNull File file); @Nullable static List<File> getFiles(@NotNull Plugin plugin, @NotNull String... subPath); @Nullable static List<File> getFiles(@NotNull File folder); @Nullable static List<String> getFileNames(@NotNull Plugin plugin, @NotNull String... subPath); @NotNull static List<String> getFileNames(@NotNull List<File> files); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull Plugin plugin, @NotNull String... children); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull File file); @Nullable static InputStream getInternalFileStream(@NotNull String... absPath); @Nullable static InputStream getInternalFileStream(@NotNull Class<?> resourceClass, @NotNull String... absPath); @Nullable static String readInternalFile(@NotNull String... absPath); @Nullable static String readInternalFile(@NotNull Class<?> resourceClass, @NotNull String... absPath); static Logger logger; }### Answer: @Test public void getFileNamesIgnoresFolders() throws IOException { Random random = new Random(); int nrOfFiles = random.nextInt(5) + 40; List<String> createdFiles = new ArrayList<>(); for (int i = 0; i < nrOfFiles; i++) { File file = new File(plugin.getDataFolder(), "file" + i); if (random.nextBoolean()) { createdFiles.add(file.getName()); assertTrue(file.createNewFile()); } else { assertTrue(file.mkdir()); } } List<String> files = FileUtils.getFileNames(plugin); assertNotNull(files); Collections.sort(createdFiles); Collections.sort(files); assertEquals(createdFiles, files); }
### Question: FileUtils { @Nullable public static InputStream getInternalFileStream(@NotNull String... absPath) { return getInternalFileStream(FileUtils.class, absPath); } static boolean createParentFolders(@NotNull File file); @NotNull static File createDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); static void createFileSafely(@NotNull File file); static boolean createFolder(@NotNull Plugin plugin, @NotNull String... children); static boolean createFolderSafely(@NotNull File folder); static boolean writeFromInternal(Plugin plugin, String[] externalPath, String... internalPath); static boolean writeFromInternal(Plugin plugin, String[] externalPath, Class<?> resourceClass, String... internalPath); static boolean writeFromInternal(Plugin plugin, Class<?> resourceClass, String... path); static boolean writeFromInternal(Plugin plugin, String... path); static boolean writeFromInternal(File outFile, String... internalPath); static boolean writeFromInternal(File outFile, Class<?> resourceClass, String... internalPath); static boolean write(@NotNull String string, @NotNull Plugin plugin, @NotNull String... path); static boolean write(@NotNull String string, @NotNull File file); static boolean save(@NotNull InputStream in, @NotNull Plugin plugin, @NotNull String... path); static boolean save(@NotNull InputStream in, @NotNull File file); static String[] splitAtSlash(String path); @NotNull static String joinPath(String... path); @NotNull static String joinPath(boolean abs, String... path); @NotNull static String joinPath(char delimiter, boolean abs, String... path); static void extractInternalFolder(@NotNull Plugin plugin, @NotNull File file, @NotNull String... folderPath); @NotNull static File getDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); @Nullable static String read(@NotNull Plugin plugin, @NotNull String... path); @Nullable static String read(@NotNull File file); @Nullable static List<File> getFiles(@NotNull Plugin plugin, @NotNull String... subPath); @Nullable static List<File> getFiles(@NotNull File folder); @Nullable static List<String> getFileNames(@NotNull Plugin plugin, @NotNull String... subPath); @NotNull static List<String> getFileNames(@NotNull List<File> files); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull Plugin plugin, @NotNull String... children); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull File file); @Nullable static InputStream getInternalFileStream(@NotNull String... absPath); @Nullable static InputStream getInternalFileStream(@NotNull Class<?> resourceClass, @NotNull String... absPath); @Nullable static String readInternalFile(@NotNull String... absPath); @Nullable static String readInternalFile(@NotNull Class<?> resourceClass, @NotNull String... absPath); static Logger logger; }### Answer: @Test public void getInternalFileStreamBinFileEquals() throws IOException { String s = "/" + String.join("/", TEST_BIN_FILE_WITH_DIR_PATH); InputStream isE = FileUtilsTest.class.getResourceAsStream(s); assertNotNull(isE); InputStream isA = FileUtils.getInternalFileStream(TEST_BIN_FILE_WITH_DIR_PATH); assertNotNull(isA); assertArrayEquals(IOUtils.toByteArray(isE), IOUtils.toByteArray(isA)); }
### Question: FileUtils { @Nullable public static String readInternalFile(@NotNull String... absPath) { return readInternalFile(FileUtils.class, absPath); } static boolean createParentFolders(@NotNull File file); @NotNull static File createDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); static void createFileSafely(@NotNull File file); static boolean createFolder(@NotNull Plugin plugin, @NotNull String... children); static boolean createFolderSafely(@NotNull File folder); static boolean writeFromInternal(Plugin plugin, String[] externalPath, String... internalPath); static boolean writeFromInternal(Plugin plugin, String[] externalPath, Class<?> resourceClass, String... internalPath); static boolean writeFromInternal(Plugin plugin, Class<?> resourceClass, String... path); static boolean writeFromInternal(Plugin plugin, String... path); static boolean writeFromInternal(File outFile, String... internalPath); static boolean writeFromInternal(File outFile, Class<?> resourceClass, String... internalPath); static boolean write(@NotNull String string, @NotNull Plugin plugin, @NotNull String... path); static boolean write(@NotNull String string, @NotNull File file); static boolean save(@NotNull InputStream in, @NotNull Plugin plugin, @NotNull String... path); static boolean save(@NotNull InputStream in, @NotNull File file); static String[] splitAtSlash(String path); @NotNull static String joinPath(String... path); @NotNull static String joinPath(boolean abs, String... path); @NotNull static String joinPath(char delimiter, boolean abs, String... path); static void extractInternalFolder(@NotNull Plugin plugin, @NotNull File file, @NotNull String... folderPath); @NotNull static File getDatafolderFile(@NotNull Plugin plugin, @NotNull String... children); @Nullable static String read(@NotNull Plugin plugin, @NotNull String... path); @Nullable static String read(@NotNull File file); @Nullable static List<File> getFiles(@NotNull Plugin plugin, @NotNull String... subPath); @Nullable static List<File> getFiles(@NotNull File folder); @Nullable static List<String> getFileNames(@NotNull Plugin plugin, @NotNull String... subPath); @NotNull static List<String> getFileNames(@NotNull List<File> files); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull Plugin plugin, @NotNull String... children); @NotNull static List<File> getRecursiveFiles(boolean excludeHyphenPrefix, @NotNull File file); @Nullable static InputStream getInternalFileStream(@NotNull String... absPath); @Nullable static InputStream getInternalFileStream(@NotNull Class<?> resourceClass, @NotNull String... absPath); @Nullable static String readInternalFile(@NotNull String... absPath); @Nullable static String readInternalFile(@NotNull Class<?> resourceClass, @NotNull String... absPath); static Logger logger; }### Answer: @Test public void readInternalFileReadsTestFileCorrectly() throws IOException { String s = "/" + String.join("/", TEST_FILE_WITH_DIR_PATH); InputStream is = FileUtilsTest.class.getResourceAsStream(s); assertNotNull(is); String fileContent = IOUtils.toString(is); assertEquals(fileContent, FileUtils.readInternalFile(TEST_FILE_WITH_DIR_PATH)); } @Test public void readInternalFileReadsTestFileCorrectly2() throws IOException { String s = "/" + String.join("/", TEST_FILE_WITHOUT_DIR_PATH); InputStream is = FileUtilsTest.class.getResourceAsStream(s); assertNotNull(is); String fileContent = IOUtils.toString(is); assertEquals(fileContent, FileUtils.readInternalFile(TEST_FILE_WITHOUT_DIR_PATH)); } @Test public void readInternalFileHandleNonExistingFileCorrectly() throws IOException { assertNull(FileUtils.readInternalFile("non-existing")); }
### Question: InventoryUtil { public static int count(@NotNull Inventory inv, @NotNull ItemStack item) { int amount = 0; for (ItemStack invItem : inv.getContents()) { if (item.isSimilar(invItem)) { amount += invItem.getAmount(); } } return amount; } @SuppressWarnings("deprecation") static void removeInvItem(@NotNull final Inventory inv, @NotNull final Material type, final int amount, final MaterialData data); static void removeInvItem(final Inventory inv, final ItemStack item, final boolean matOnly); static void removeInvItem(@NotNull final Inventory inv, @NotNull final ItemStack item); static void removeInvMat(@NotNull final Inventory inv, @NotNull final Material material, final int amount); static boolean contains(@Nullable final Inventory inv, @Nullable final ItemStack item, int amount); static int getTotalItems(final Inventory inv, final Material type); static int countItems(@NotNull final Player player, final Material material); static int countItems(@NotNull final Player player, final Material material, final short damageValue); static int count(@NotNull Inventory inv, @NotNull ItemStack item); @SuppressWarnings("deprecation") static boolean canHold(@NotNull final Player player, final ItemStack item); static boolean canHold(final Player player, @NotNull final Material material, final byte data, final int quantity); static int emptySlots(final Inventory inv); @NotNull static ItemStack getPlayerSkull(@NotNull final OfflinePlayer player); static ItemStack[] getPlayerArmor(@Nullable Inventory inv); }### Answer: @Test public void testCountOneEqualElement() { int amount = 16; ItemStack item = new ItemStack(Material.APPLE, amount); Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {item}); Assert.assertEquals(amount, InventoryUtil.count(inv, item)); } @Test public void testCountNoItems() { Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {}); Assert.assertEquals(0, InventoryUtil.count(inv, new ItemStack(Material.APPLE))); } @Test public void testCountDuplicate() { int amount = 16; ItemStack item = new ItemStack(Material.APPLE, amount); Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {item, item}); Assert.assertEquals(2 * amount, InventoryUtil.count(inv, item)); } @Test public void testCountDuplicateNewInstance() { int amount = 16; ItemStack itemA = new ItemStack(Material.APPLE, amount); ItemStack itemB = new ItemStack(Material.APPLE, amount * 2); Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {itemA, itemB}); assertTrue(itemA.isSimilar(itemB)); Assert.assertEquals(amount * 3, InventoryUtil.count(inv, itemA)); } @Test public void testCountOtherItems() { int amount = 16; ItemStack itemA = new ItemStack(Material.APPLE, amount); ItemStack itemB = new ItemStack(Material.GOLDEN_APPLE, amount / 2); Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {itemA, itemB}); Assert.assertEquals(amount, InventoryUtil.count(inv, itemA)); }
### Question: ConfigUtil { public static void saveDefaultResources(@NotNull Plugin plugin, @NotNull String... resources) throws IOException { saveDefaultResources(plugin, true, resources); } static void saveDefaultResources(@NotNull Plugin plugin, @NotNull String... resources); static void saveDefaultResources(@NotNull Plugin plugin, boolean create, @NotNull String... resources); static void saveDefaultResources(@NotNull Plugin plugin, @NotNull Class<?> resourceJar, boolean create, @NotNull String... resources); @Nullable static FileConfiguration getYaml(@NotNull Plugin plugin, String... filename); @Nullable static FileConfiguration getYaml(@NotNull File file); @NotNull static FileConfiguration getYamlOrDefault(@NotNull Plugin plugin, @NotNull FileConfiguration def, String... filename); @NotNull static FileConfiguration getYamlOrDefault(@NotNull File file, @NotNull FileConfiguration def); static boolean saveYaml(@NotNull Plugin plugin, FileConfiguration conf, String... savePath); static boolean saveYaml(@Nullable ConfigurationSection conf, @Nullable File file); @NotNull static Map<String, Object> getMapSection(@NotNull ConfigurationSection conf, @NotNull String path); @NotNull static Map<String, Object> getMapSection(@Nullable Object obj); @NotNull static ConfigurationSection getSection(@NotNull Object obj); @NotNull static ConfigurationSection getSectionFromMap(@NotNull Map<String, Object> map); static boolean isEmpty(@Nullable ConfigurationSection conf); @NotNull static FileConfiguration toFileConf(@NotNull ConfigurationSection conf); static String saveToString(@NotNull ConfigurationSection conf); @NotNull static YamlConfiguration loadFromString(@NotNull String yaml); @Nullable static YamlConfiguration loadFromStringOrNull(@NotNull String yaml); static Set<String> flatKeys(@NotNull ConfigurationSection conf); @Nullable static T get(@NotNull ConfigurationSection conf, @NotNull String path); @Nullable @Contract("_,_,!null->!null") static T get(@NotNull ConfigurationSection conf, @NotNull String path, @Nullable T fallback); @NotNull static ConfigurationSection diff(@NotNull ConfigurationSection confA, @NotNull ConfigurationSection confB); static Logger logger; }### Answer: @Test(expected = FileNotFoundException.class) public void saveDefaultResourcesNotExistingWhenNotCreating() throws IOException { ConfigUtil.saveDefaultResources(plugin, false, NON_EXISTING_TEST_FILE_WITH_DIR_PATH); } @Test public void saveDefaultResourcesDoesNotOverwrite() throws IOException { String content = "yaegstfiuaeifyefgsaiefgipsaefåøæ"; FileUtils.write(content, plugin, TEST_FILE_WITH_DIR_PATH); ConfigUtil.saveDefaultResources(plugin, TEST_FILE_WITH_DIR_PATH); assertEquals(content, FileUtils.read(plugin, TEST_FILE_WITH_DIR_PATH)); } @Test public void saveDefaultResourcesSavesMultipleFiles() throws IOException { String[] filesPaths = {TEST_FILE_WITH_DIR_PATH, TEST_FILE_WITHOUT_DIR_PATH, NON_EXISTING_TEST_FILE_WITH_DIR_PATH}; List<File> expectedFiles = new ArrayList<>(); for (String path : filesPaths) { expectedFiles.add(FileUtils.getDatafolderFile(plugin, path.split("[/\\\\]"))); } ConfigUtil.saveDefaultResources(plugin, filesPaths); List<File> savedFiles = FileUtils.getRecursiveFiles(false, plugin); Collections.sort(expectedFiles); Collections.sort(savedFiles); assertEquals(expectedFiles, savedFiles); for (File file : savedFiles) { assertTrue("File " + file + " does not exist", file.exists()); } }
### Question: WorldLoader { @Nullable public static Location worldlessBlockLocationFromConfig(@NotNull ConfigurationSection conf) { try { int x = Integer.parseInt(conf.getString(X)); int y = Integer.parseInt(conf.getString(Y)); int z = Integer.parseInt(conf.getString(Z)); return new Location(null, x, y, z); } catch (NumberFormatException ex) { return null; } } @NotNull static ConfigurationSection locationToConfig(@NotNull Location location, boolean useUUID); @Nullable static Location locationFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); @NotNull static ConfigurationSection blockLocationToConfig(@NotNull Location location, boolean useUUID); @Nullable static Location blockLocationFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); @Nullable static Location worldlessBlockLocationFromConfig(@NotNull ConfigurationSection conf); @Nullable static World worldFromConfig(@NotNull ConfigurationSection conf, boolean useUUID); static final String WORLD_UID; static final String WORLD_NAME; static final String X; static final String Y; static final String Z; static final String YAW; static final String PITCH; }### Answer: @Test public void worldlessBlockLocFromConfEmpty() { Location loc = WorldLoader.worldlessBlockLocationFromConfig(new YamlConfiguration()); Assert.assertNull(loc); } @Test public void worldlessBlockLocFromConfValid() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.X, 4); conf.set(WorldLoader.Y, 0); conf.set(WorldLoader.Z, -4); Location loc = WorldLoader.worldlessBlockLocationFromConfig(conf); Assert.assertNotNull(loc); } @Test public void worldlessBlockLocFromConfNotAllKeys() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.X, 4); conf.set(WorldLoader.Z, -4); Location loc = WorldLoader.worldlessBlockLocationFromConfig(conf); Assert.assertNull(loc); } @Test public void worldlessBlockLocFromConfWrongType() { YamlConfiguration conf = new YamlConfiguration(); conf.set(WorldLoader.X, "aaaaaa"); conf.set(WorldLoader.Y, 0); conf.set(WorldLoader.Z, -4); Location loc = WorldLoader.worldlessBlockLocationFromConfig(conf); Assert.assertNull(loc); }
### Question: MainPresenter extends BaseTypedPresenter<String, Throwable> { @Override public void onViewResumed() { getView().showData("This is some awesome data"); getView().showError(new RuntimeException("Exceptional Error")); } MainPresenter(ITypedView<String, Throwable> view); @Override void onViewResumed(); }### Answer: @Test public void testShowDataOnResume() { mPresenter.onViewResumed(); verify(mView).showData(anyString()); } @Test public void testShowErrorOnResume() { mPresenter.onViewResumed(); verify(mView).showError(any(Throwable.class)); }
### Question: EclipseCollectionsDeckOfCardsAsList { public ImmutableList<Card> getCards() { return this.cards; } EclipseCollectionsDeckOfCardsAsList(); MutableStack<Card> shuffle(Random random); MutableSet<Card> deal(MutableStack<Card> stack, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( MutableStack<Card> shuffled, int hands, int cardsPerHand); ImmutableList<Card> diamonds(); ImmutableList<Card> hearts(); ImmutableList<Card> spades(); ImmutableList<Card> clubs(); Bag<Suit> countsBySuit(); Bag<Rank> countsByRank(); ImmutableList<Card> getCards(); ImmutableListMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void allCards() { Assert.assertEquals(this.jdkDeck.getCards(), this.ecDeck.getCards()); } @Test public void cardsAreImmutable() { var ecCards = this.ecDeck.getCards().castToList(); Verify.assertThrows( UnsupportedOperationException.class, () -> ecCards.remove(0)); Verify.assertThrows( UnsupportedOperationException.class, ecCards::clear); Verify.assertThrows( UnsupportedOperationException.class, () -> ecCards.add(null)); }
### Question: HashBag implements MutableBag<T> { public void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer) { Counter index = new Counter(); this.backingMap.forEach((key, count) -> { for (int i = 0; i < count; i++) { biConsumer.accept(key, index.getCount()); index.increment(); } }); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void forEachWithIndex() { MutableList<String> list = MutableList.empty(); this.testObj.withAll("1", "2", "3", "2", "3", "3") .forEachWithIndex((each, index) -> list.add(each + index)); Assert.assertEquals(MutableList.of("10", "21", "22", "33", "34", "35"), list); }
### Question: HashBag implements MutableBag<T> { @Override public boolean add(T element) { this.backingMap.merge(element, 1, (existingValue, newValue) -> existingValue + 1); this.size++; return true; } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void add() { this.testObj.add("1"); this.testObj.add("2"); this.testObj.add("2"); Assert.assertEquals(MutableBag.of("1", "2", "2"), this.testObj); }
### Question: HashBag implements MutableBag<T> { @Override public boolean remove(Object o) { return this.removeOccurrences((T) o, Integer.MAX_VALUE); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void remove() { this.testObj.add("1"); this.testObj.add("2"); this.testObj.add("2"); Assert.assertEquals(MutableBag.of("1", "2", "2"), this.testObj); this.testObj.remove("2"); Assert.assertEquals(MutableBag.of("1"), this.testObj); this.testObj.remove("1"); Assert.assertEquals(MutableBag.empty(), this.testObj); }
### Question: HashBag implements MutableBag<T> { @Override public boolean containsAll(Collection<?> c) { return c.stream().allMatch(each -> this.backingMap.containsKey(each)); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void containsAll() { Assert.assertTrue(this.testObj.containsAll(MutableList.empty())); Assert.assertFalse(this.testObj.containsAll(MutableList.of("1"))); this.testObj.withAll("1", "2", "3", "2", "3", "3"); Assert.assertTrue(this.testObj.containsAll(MutableList.of("1"))); Assert.assertTrue(this.testObj.containsAll(MutableList.of("1", "2"))); Assert.assertFalse(this.testObj.containsAll(MutableList.of("1", "2", "4"))); }
### Question: HashBag implements MutableBag<T> { @Override public boolean addAll(Collection<? extends T> c) { int sizeBefore = this.size; c.forEach(each -> this.addOccurrence(each)); return sizeBefore != this.size; } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void addAll() { Assert.assertFalse(this.testObj.addAll(MutableList.empty())); Assert.assertEquals(MutableBag.empty(), this.testObj); Assert.assertTrue(this.testObj.addAll(MutableList.of("1", "2", "2"))); Assert.assertEquals(MutableBag.of("1", "2", "2"), this.testObj); }
### Question: HashBag implements MutableBag<T> { @Override public boolean removeAll(Collection<?> c) { int sizeBefore = this.size; c.forEach(each -> this.removeOccurrences((T) each, Integer.MAX_VALUE)); return sizeBefore != this.size; } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void removeAll() { Assert.assertFalse(this.testObj.removeAll(MutableList.empty())); this.testObj.withAll("1", "2", "3", "2", "3", "3"); Assert.assertTrue(this.testObj.removeAll(MutableList.of("1", "2"))); Assert.assertEquals(MutableBag.of("3", "3", "3"), this.testObj); Assert.assertFalse(this.testObj.removeAll(MutableList.of("4"))); Assert.assertTrue(this.testObj.removeAll(MutableList.of("3", "3", "3"))); Assert.assertEquals(MutableBag.empty(), this.testObj); }
### Question: HashBag implements MutableBag<T> { @Override public void clear() { this.backingMap.clear(); this.size = 0; } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void clear() { this.testObj.withAll("1", "2", "3", "2", "3", "3"); Assert.assertFalse(this.testObj.isEmpty()); this.testObj.clear(); Assert.assertTrue(this.testObj.isEmpty()); Assert.assertEquals(0, this.testObj.size()); Assert.assertEquals(0, this.testObj.sizeDistinct()); }
### Question: HashBag implements MutableBag<T> { @Override public int getOccurrences(T element) { return this.backingMap.getOrDefault(element, 0); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void getOccurrences() { this.testObj.withAll("1", "2", "3", "2", "3", "3"); Assert.assertEquals(1, this.testObj.getOccurrences("1")); Assert.assertEquals(2, this.testObj.getOccurrences("2")); Assert.assertEquals(3, this.testObj.getOccurrences("3")); Assert.assertEquals(0, this.testObj.getOccurrences("4")); }
### Question: HashBag implements MutableBag<T> { @Override public Iterator<T> iterator() { return new BagIterator(); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void iterator() { this.testObj.withAll("1", "2", "3", "2", "3", "3"); Iterator<String> iterator = this.testObj.iterator(); Assert.assertEquals("1", iterator.next()); Assert.assertTrue(iterator.hasNext()); iterator.remove(); Assert.assertEquals("2", iterator.next()); iterator.remove(); Assert.assertEquals("2", iterator.next()); Assert.assertEquals("3", iterator.next()); iterator.remove(); Assert.assertEquals("3", iterator.next()); iterator.remove(); Assert.assertEquals("3", iterator.next()); iterator.remove(); Assert.assertFalse(iterator.hasNext()); Assert.assertEquals(MutableBag.of("2"), this.testObj); }
### Question: HashBag implements MutableBag<T> { @Override public boolean addOccurrence(T element) { return this.addOccurrences(element, 1); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void addOccurrence() { this.testObj.addOccurrence("1"); Assert.assertEquals(MutableBag.of("1"), this.testObj); Assert.assertEquals(1, this.testObj.size()); Assert.assertEquals(1, this.testObj.sizeDistinct()); }
### Question: EclipseCollectionsDeckOfCardsAsList { public ImmutableListMultimap<Suit, Card> getCardsBySuit() { return this.cardsBySuit; } EclipseCollectionsDeckOfCardsAsList(); MutableStack<Card> shuffle(Random random); MutableSet<Card> deal(MutableStack<Card> stack, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( MutableStack<Card> shuffled, int hands, int cardsPerHand); ImmutableList<Card> diamonds(); ImmutableList<Card> hearts(); ImmutableList<Card> spades(); ImmutableList<Card> clubs(); Bag<Suit> countsBySuit(); Bag<Rank> countsByRank(); ImmutableList<Card> getCards(); ImmutableListMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void cardsBySuitIsImmutable() { var ecCardsBySuit = this.ecDeck.getCardsBySuit(); Verify.assertThrows( UnsupportedOperationException.class, () -> ecCardsBySuit.get(Suit.CLUBS).castToList().remove(0)); Verify.assertThrows( UnsupportedOperationException.class, () -> ecCardsBySuit.get(Suit.CLUBS).castToList().add(null)); Verify.assertThrows( UnsupportedOperationException.class, ecCardsBySuit.get(Suit.CLUBS).castToList()::clear); } @Test public void cardsBySuit() { var ecCardsBySuit = this.ecDeck.getCardsBySuit(); var jdkCardsBySuit = this.jdkDeck.getCardsBySuit(); Assert.assertEquals(jdkCardsBySuit.get(Suit.CLUBS), ecCardsBySuit.get(Suit.CLUBS)); }
### Question: HashBag implements MutableBag<T> { @Override public boolean addOccurrences(T element, int occurrences) { int sizeBefore = size; Integer merged = this.backingMap.merge(element, occurrences, (existingCount, e) -> existingCount + e); size = size + occurrences; return sizeBefore != size; } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void addOccurrences() { this.testObj.addOccurrences("1", 1); Assert.assertEquals(MutableBag.of("1"), this.testObj); Assert.assertEquals(1, this.testObj.size()); Assert.assertEquals(1, this.testObj.sizeDistinct()); this.testObj.addOccurrences("2", 2); Assert.assertEquals(MutableBag.of("1", "2", "2"), this.testObj); Assert.assertEquals(3, this.testObj.size()); Assert.assertEquals(2, this.testObj.sizeDistinct()); }
### Question: HashBag implements MutableBag<T> { @Override public boolean removeOccurrence(T element) { return this.removeOccurrences(element, 1); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void removeOccurrence() { Assert.assertFalse(this.testObj.removeOccurrence("1")); Assert.assertEquals(MutableBag.empty(), this.testObj); this.testObj.withAll("1", "2", "3", "2", "3", "3"); Assert.assertTrue(this.testObj.removeOccurrence("1")); Assert.assertEquals(MutableBag.of("2", "2", "3", "3", "3"), this.testObj); Assert.assertTrue(this.testObj.removeOccurrence("2")); Assert.assertEquals(MutableBag.of("2", "3", "3", "3"), this.testObj); Assert.assertTrue(this.testObj.removeOccurrence("2")); Assert.assertEquals(MutableBag.of("3", "3", "3"), this.testObj); Assert.assertFalse(this.testObj.removeOccurrence("2")); }
### Question: HashBag implements MutableBag<T> { @Override public boolean removeOccurrences(T element, int occurrences) { int sizeBefore = size; Integer existing = this.backingMap.get(element); if (existing != null) { Integer newCount = existing - occurrences; if (newCount <= 0) { this.backingMap.remove(element); size = size - existing; } else { this.backingMap.put(element, newCount); size = size - occurrences; } } return sizeBefore != size; } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void removeOccurrences() { Assert.assertFalse(this.testObj.removeOccurrences("1", 1)); Assert.assertEquals(MutableBag.empty(), this.testObj); this.testObj.withAll("1", "2", "3", "2", "3", "3"); Assert.assertTrue(this.testObj.removeOccurrences("1", 2)); Assert.assertEquals(MutableBag.of("2", "2", "3", "3", "3"), this.testObj); Assert.assertEquals(5, this.testObj.size()); Assert.assertTrue(this.testObj.removeOccurrences("2", 2)); Assert.assertEquals(MutableBag.of("3", "3", "3"), this.testObj); Assert.assertFalse(this.testObj.removeOccurrences("2", 2)); }
### Question: HashBag implements MutableBag<T> { @Override public void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer) { this.backingMap.forEach(biConsumer); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void forEachWithOccurrences() { MutableList<String> list = MutableList.empty(); this.testObj.withAll("1", "2", "3", "2", "3", "3") .forEachWithOccurrences((each, count) -> list.add(each + count)); Assert.assertEquals(MutableList.of("11", "22", "33"), list); }
### Question: HashBag implements MutableBag<T> { @Override public void forEach(Consumer<? super T> consumer) { this.backingMap.forEach((key, count) -> { for (int i = 0; i < count; i++) { consumer.accept(key); } }); } @Override int sizeDistinct(); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Object[] toArray(); void forEachWithIndex(BiConsumer<? super T, Integer> biConsumer); @Override T1[] toArray(T1[] array); @Override boolean add(T element); @Override boolean remove(Object o); @Override boolean containsAll(Collection<?> c); @Override boolean addAll(Collection<? extends T> c); @Override boolean removeAll(Collection<?> c); @Override boolean retainAll(Collection<?> c); @Override void clear(); @Override Iterator<T> iterator(); @Override int getOccurrences(T element); @Override boolean addOccurrence(T element); @Override boolean addOccurrences(T element, int occurrences); @Override boolean removeOccurrence(T element); @Override boolean removeOccurrences(T element, int occurrences); @Override void forEachWithOccurrences(BiConsumer<? super T, Integer> biConsumer); @Override void forEach(Consumer<? super T> consumer); @Override int hashCode(); @Override boolean equals(Object other); }### Answer: @Test public void forEach() { MutableList<String> list = MutableList.empty(); this.testObj.withAll("1", "2", "3", "2", "3", "3") .forEach((each) -> list.add(each)); Assert.assertEquals(MutableList.of("1", "2", "2", "3", "3", "3"), list); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public boolean isEmpty() { return this.BACKING_MULTIMAP.isEmpty(); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void isEmpty() { this.testObj = new UnmodifiableArrayListMultimap(new ArrayListMultimap()); Assert.assertTrue(this.testObj.isEmpty()); this.testObj = new UnmodifiableArrayListMultimap(this.inputMultimap); Assert.assertFalse(this.testObj.isEmpty()); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public MutableList<V> get(Object key) { return this.BACKING_MULTIMAP.get(key).asUnmodifiable(); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void get() { MutableList<Integer> value = this.testObj.get("A"); Assert.assertEquals(MutableList.of(1, 2, 3, 1), value); Assert.assertEquals(MutableList.empty(), this.testObj.get("C")); Verify.assertThrows(UnsupportedOperationException.class, () -> value.add(6)); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public boolean put(K key, RichIterable<V> value) { throw new UnsupportedOperationException("Not allowed to perform put() on " + UnmodifiableArrayListMultimap.class); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void put() { Verify.assertThrows(UnsupportedOperationException.class, () -> this.testObj.put("A", 1)); } @Test public void get_putIterable() { Verify.assertThrows(UnsupportedOperationException.class, () -> this.testObj.put("A", MutableList.of(1))); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public void putAll(Map<? extends K, ? extends RichIterable<V>> map) { throw new UnsupportedOperationException("Not allowed to perform putAll() on " + UnmodifiableArrayListMultimap.class); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void putAll() { Verify.assertThrows(UnsupportedOperationException.class, () -> this.testObj.putAll(MutableMap.of("A", MutableList.of(1, 2, 3)))); }
### Question: EclipseCollectionsDeckOfCardsAsList { public Bag<Suit> countsBySuit() { return null; } EclipseCollectionsDeckOfCardsAsList(); MutableStack<Card> shuffle(Random random); MutableSet<Card> deal(MutableStack<Card> stack, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( MutableStack<Card> shuffled, int hands, int cardsPerHand); ImmutableList<Card> diamonds(); ImmutableList<Card> hearts(); ImmutableList<Card> spades(); ImmutableList<Card> clubs(); Bag<Suit> countsBySuit(); Bag<Rank> countsByRank(); ImmutableList<Card> getCards(); ImmutableListMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void countsBySuit() { Assert.assertEquals( this.jdkDeck.countsBySuit().get(Suit.CLUBS).intValue(), this.ecDeck.countsBySuit().occurrencesOf(Suit.CLUBS)); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public MutableList<V> remove(Object key) { throw new UnsupportedOperationException("Not allowed to perform remove() on " + UnmodifiableArrayListMultimap.class); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void remove() { Verify.assertThrows(UnsupportedOperationException.class, () -> this.testObj.remove("A")); } @Test public void remove_keyValue() { Verify.assertThrows(UnsupportedOperationException.class, () -> this.testObj.remove("A")); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public boolean containsKey(Object key) { return this.BACKING_MULTIMAP.containsKey(key); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void containsKey() { Assert.assertTrue(this.testObj.containsKey("A")); Assert.assertTrue(this.testObj.containsKey("B")); Assert.assertFalse(this.testObj.containsKey("C")); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public boolean containsValue(Object value) { return this.BACKING_MULTIMAP.containsValue(value); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void containsValue() { Assert.assertTrue(this.testObj.containsValue(1)); Assert.assertTrue(this.testObj.containsValue(2)); Assert.assertTrue(this.testObj.containsValue(3)); Assert.assertTrue(this.testObj.containsValue(10)); Assert.assertFalse(this.testObj.containsValue(4)); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public void clear() { throw new UnsupportedOperationException("Not allowed to perform clear() on " + UnmodifiableArrayListMultimap.class); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void clear() { Verify.assertThrows(UnsupportedOperationException.class, () -> this.testObj.clear()); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public Set<K> keySet() { return this.BACKING_MULTIMAP.keySet(); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void keySet() { Assert.assertEquals(MutableSet.of("A", "B"), this.testObj.keySet()); }
### Question: UnmodifiableArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { @Override public void forEach(BiConsumer<K, V> biConsumer) { this.BACKING_MULTIMAP.forEach(biConsumer); } UnmodifiableArrayListMultimap(MutableMultimap<K, V> multimap); @Override MutableListMultimap<K, V> asUnmodifiable(); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override void clear(); @Override Set<K> keySet(); @Override MutableList<V> get(Object key); @Override void forEach(BiConsumer<K, V> biConsumer); @Override boolean put(K key, RichIterable<V> value); @Override boolean put(K key, V value); @Override MutableList<V> remove(Object key); @Override boolean remove(K key, V value); @Override void putAll(Map<? extends K, ? extends RichIterable<V>> map); @Override int hashCode(); @Override boolean equals(Object obj); }### Answer: @Test public void forEach() { MutableList<String> combined = MutableList.empty(); this.testObj.forEach((key, value) -> combined.add(key + value.toString())); Assert.assertEquals( MutableList.of("A1", "A2", "A3", "A1", "B10", "B10"), combined); }
### Question: EclipseCollectionsDeckOfCardsAsList { public Bag<Rank> countsByRank() { return null; } EclipseCollectionsDeckOfCardsAsList(); MutableStack<Card> shuffle(Random random); MutableSet<Card> deal(MutableStack<Card> stack, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( MutableStack<Card> shuffled, int hands, int cardsPerHand); ImmutableList<Card> diamonds(); ImmutableList<Card> hearts(); ImmutableList<Card> spades(); ImmutableList<Card> clubs(); Bag<Suit> countsBySuit(); Bag<Rank> countsByRank(); ImmutableList<Card> getCards(); ImmutableListMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void countsByRank() { Assert.assertEquals( this.jdkDeck.countsByRank().get(Rank.TEN).intValue(), this.ecDeck.countsByRank().occurrencesOf(Rank.SEVEN)); }
### Question: ArrayListMultimap extends AbstractMutableMultimap<K, V, MutableList<V>> implements MutableListMultimap<K, V> { public static <K, V> ArrayListMultimap<K, V> newMultimap() { return new ArrayListMultimap<>(); } static ArrayListMultimap<K, V> newMultimap(); @Override int size(); MutableListMultimap<K, V> asUnmodifiable(); }### Answer: @Test public void hashCodeEquals() { this.testObj.putAll(MutableMap.of("A", MutableList.of(1, 2, 3, 1))); this.testObj.putAll(MutableMap.of("B", MutableList.of(10, 10))); ArrayListMultimap<String, Integer> other = ArrayListMultimap.newMultimap(); other.putAll(MutableMap.of("A", MutableList.of(1, 2, 3, 1))); Assert.assertTrue(this.testObj.equals(this.testObj)); Assert.assertFalse(this.testObj.equals(other)); Assert.assertFalse(other.equals(this.testObj)); other.putAll(MutableMap.of("B", MutableList.of(10, 10))); Verify.assertEqualsAndHashCode(this.testObj, this.testObj); Verify.assertEqualsAndHashCode(this.testObj, other); Verify.assertEqualsAndHashCode(other, this.testObj); }
### Question: JDK8DeckOfCardsAsSortedSet { public SortedSet<Card> getCards() { return this.cards; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void allCards() { Assert.assertEquals(this.jdk1Deck.getCards(), this.jdk2Deck.getCards()); } @Test public void cardsAreImmutable() { var jdk2Cards = this.jdk2Deck.getCards(); Verify.assertThrows( UnsupportedOperationException.class, () -> jdk2Cards.remove(null)); Verify.assertThrows( UnsupportedOperationException.class, jdk2Cards::clear); Verify.assertThrows( UnsupportedOperationException.class, () -> jdk2Cards.add(null)); }
### Question: JDK8DeckOfCardsAsSortedSet { public SortedSet<Card> diamonds() { return null; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void diamonds() { Assert.assertEquals(this.jdk1Deck.diamonds(), this.jdk2Deck.diamonds()); }
### Question: JDK8DeckOfCardsAsSortedSet { public SortedSet<Card> hearts() { return null; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void hearts() { Assert.assertEquals(this.jdk1Deck.hearts(), this.jdk2Deck.hearts()); }
### Question: JDK8DeckOfCardsAsSortedSet { public SortedSet<Card> spades() { return null; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void spades() { Assert.assertEquals(this.jdk1Deck.spades(), this.jdk2Deck.spades()); }
### Question: JDK8DeckOfCardsAsSortedSet { public SortedSet<Card> clubs() { return null; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void clubs() { Assert.assertEquals(this.jdk1Deck.clubs(), this.jdk2Deck.clubs()); }
### Question: JDK8DeckOfCardsAsList { public List<Card> getCards() { return this.cards; } JDK8DeckOfCardsAsList(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands(Deque<Card> shuffled, int hands, int cardsPerHand); List<Card> diamonds(); List<Card> hearts(); List<Card> spades(); List<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); List<Card> getCards(); Map<Suit, List<Card>> getCardsBySuit(); }### Answer: @Test public void allCards() { Assert.assertEquals(this.jdk1Deck.getCards(), this.jdk2Deck.getCards()); } @Test public void cardsAreImmutable() { var jdk2Cards = this.jdk2Deck.getCards(); Verify.assertThrows( UnsupportedOperationException.class, () -> jdk2Cards.remove(0)); Verify.assertThrows( UnsupportedOperationException.class, jdk2Cards::clear); Verify.assertThrows( UnsupportedOperationException.class, () -> jdk2Cards.add(null)); }
### Question: JDK8DeckOfCardsAsSortedSet { public Set<Card> deal(Deque<Card> deque, int count) { Set<Card> hand = new HashSet<>(); IntStream.range(0, count).forEach(i -> hand.add(deque.pop())); return hand; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void deal() { Deque<Card> jdk1Shuffle = this.jdk1Deck.shuffle(new Random(1)); Deque<Card> jdk2Shuffle = this.jdk2Deck.shuffle(new Random(1)); Set<Card> jdk1Hand = this.jdk1Deck.deal(jdk1Shuffle, 5); Set<Card> jdk2Hand = this.jdk2Deck.deal(jdk2Shuffle, 5); Assert.assertEquals(jdk1Hand, jdk2Hand); }
### Question: JDK8DeckOfCardsAsSortedSet { public List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand) { Deque<Card> shuffled = this.shuffle(random); return this.dealHands(shuffled, hands, cardsPerHand); } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void shuffleAndDealHands() { List<Set<Card>> jdk1Hands = this.jdk1Deck.shuffleAndDeal(new Random(1), 5, 5); List<Set<Card>> jdk2Hands = this.jdk2Deck.shuffleAndDeal(new Random(1), 5, 5); Assert.assertEquals(jdk1Hands, jdk2Hands); }
### Question: JDK8DeckOfCardsAsSortedSet { public List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand) { return null; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void dealHands() { Deque<Card> jdk1Shuffled = this.jdk1Deck.shuffle(new Random(1)); Deque<Card> jdk2Shuffled = this.jdk2Deck.shuffle(new Random(1)); List<Set<Card>> jdk1Hands = this.jdk1Deck.dealHands(jdk1Shuffled, 5, 5); List<Set<Card>> jdk2Hands = this.jdk2Deck.dealHands(jdk2Shuffled, 5, 5); Assert.assertEquals(jdk1Hands, jdk2Hands); }
### Question: JDK8DeckOfCardsAsSortedSet { public Map<Suit, SortedSet<Card>> getCardsBySuit() { return this.cardsBySuit; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void cardsBySuit() { Map<Suit, SortedSet<Card>> jdk1CardsBySuit = this.jdk1Deck.getCardsBySuit(); Map<Suit, SortedSet<Card>> jdk2CardsBySuit = this.jdk2Deck.getCardsBySuit(); Assert.assertEquals(jdk1CardsBySuit.get(Suit.CLUBS), jdk2CardsBySuit.get(Suit.CLUBS)); } @Test public void cardsBySuitIsImmutable() { var jdk2CardsBySuit = this.jdk2Deck.getCardsBySuit(); Verify.assertThrows( UnsupportedOperationException.class, () -> jdk2CardsBySuit.remove(Suit.CLUBS)); Verify.assertThrows( UnsupportedOperationException.class, jdk2CardsBySuit::clear); Verify.assertThrows( UnsupportedOperationException.class, () -> jdk2CardsBySuit.get(Suit.CLUBS).remove(null)); Verify.assertThrows( UnsupportedOperationException.class, () -> jdk2CardsBySuit.get(Suit.CLUBS).add(null)); Verify.assertThrows( UnsupportedOperationException.class, jdk2CardsBySuit.get(Suit.CLUBS)::clear); }
### Question: JDK8DeckOfCardsAsSortedSet { public Map<Suit, Long> countsBySuit() { return null; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void countsBySuit() { Assert.assertEquals( this.jdk1Deck.countsBySuit().get(Suit.CLUBS), this.jdk2Deck.countsBySuit().get(Suit.CLUBS)); }
### Question: JDK8DeckOfCardsAsSortedSet { public Map<Rank, Long> countsByRank() { return null; } JDK8DeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void countsByRank() { Assert.assertEquals( this.jdk1Deck.countsByRank().get(Rank.TEN), this.jdk2Deck.countsByRank().get(Rank.TEN)); }
### Question: VavrDeckOfCardsAsSortedSet { public SortedSet<Card> getCards() { return this.cards; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void allCards() { Assert.assertEquals(this.jdkDeck.getCards(), this.vavrDeck.getCards().toJavaSet()); }
### Question: VavrDeckOfCardsAsSortedSet { public SortedSet<Card> diamonds() { return null; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void diamonds() { Assert.assertEquals(this.jdkDeck.diamonds(), this.vavrDeck.diamonds().toJavaSet()); }
### Question: VavrDeckOfCardsAsSortedSet { public SortedSet<Card> hearts() { return null; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void hearts() { Assert.assertEquals(this.jdkDeck.hearts(), this.vavrDeck.hearts().toJavaSet()); }
### Question: VavrDeckOfCardsAsSortedSet { public SortedSet<Card> spades() { return null; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void spades() { Assert.assertEquals(this.jdkDeck.spades(), this.vavrDeck.spades().toJavaSet()); }
### Question: VavrDeckOfCardsAsSortedSet { public SortedSet<Card> clubs() { return null; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void clubs() { Assert.assertEquals(this.jdkDeck.clubs(), this.vavrDeck.clubs().toJavaSet()); }
### Question: VavrDeckOfCardsAsSortedSet { public Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count) { Set<Card> hand = HashSet.empty(); for (int i = 0; i < count; i++) { Tuple2<Card, ? extends List<Card>> cardTuple2 = stack.pop2(); stack = cardTuple2._2(); hand = hand.add(cardTuple2._1()); } return Tuple.of(hand, stack); } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void deal() { Deque<Card> jdkShuffle = this.jdkDeck.shuffle(new Random(1)); io.vavr.collection.List<Card> vavrShuffle = this.vavrDeck.shuffle(new Random(1)); Set<Card> jdkHand = this.jdkDeck.deal(jdkShuffle, 5); Set<Card> vavrHand = this.vavrDeck.deal(vavrShuffle, 5)._1().toJavaSet(); Assert.assertEquals(jdkHand, vavrHand); }
### Question: VavrDeckOfCardsAsSortedSet { public List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand) { List<Card> shuffled = this.shuffle(random); return this.dealHands(shuffled, hands, cardsPerHand); } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void shuffleAndDealHands() { List<Set<Card>> jdkHands = this.jdkDeck.shuffleAndDeal(new Random(1), 5, 5); io.vavr.collection.List<io.vavr.collection.Set<Card>> vavrHands = this.vavrDeck.shuffleAndDeal(new Random(1), 5, 5); Assert.assertEquals(jdkHands.get(0), vavrHands.get(0).toJavaSet()); Assert.assertEquals(jdkHands.get(1), vavrHands.get(1).toJavaSet()); Assert.assertEquals(jdkHands.get(2), vavrHands.get(2).toJavaSet()); Assert.assertEquals(jdkHands.get(3), vavrHands.get(3).toJavaSet()); Assert.assertEquals(jdkHands.get(4), vavrHands.get(4).toJavaSet()); }
### Question: VavrDeckOfCardsAsSortedSet { public List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand) { return null; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void dealHands() { Deque<Card> jdkShuffled = this.jdkDeck.shuffle(new Random(1)); io.vavr.collection.List<Card> vavrShuffled = this.vavrDeck.shuffle(new Random(1)); List<Set<Card>> jdkHands = this.jdkDeck.dealHands(jdkShuffled, 5, 5); io.vavr.collection.List<io.vavr.collection.Set<Card>> vavrHands = this.vavrDeck.dealHands(vavrShuffled, 5, 5); Assert.assertEquals(jdkHands.get(0), vavrHands.get(0).toJavaSet()); Assert.assertEquals(jdkHands.get(1), vavrHands.get(1).toJavaSet()); Assert.assertEquals(jdkHands.get(2), vavrHands.get(2).toJavaSet()); Assert.assertEquals(jdkHands.get(3), vavrHands.get(3).toJavaSet()); Assert.assertEquals(jdkHands.get(4), vavrHands.get(4).toJavaSet()); }
### Question: VavrDeckOfCardsAsSortedSet { public Map<Suit, ? extends SortedSet<Card>> getCardsBySuit() { return this.cardsBySuit; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void cardsBySuit() { Map<Suit, SortedSet<Card>> jdkCardsBySuit = this.jdkDeck.getCardsBySuit(); io.vavr.collection.Map<Suit, ? extends io.vavr.collection.SortedSet<Card>> vavrCardsBySuit = this.vavrDeck.getCardsBySuit(); Assert.assertEquals(jdkCardsBySuit.get(Suit.CLUBS), vavrCardsBySuit.get(Suit.CLUBS).get().toJavaList()); }
### Question: VavrDeckOfCardsAsSortedSet { public Map<Suit, Long> countsBySuit() { return null; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void countsBySuit() { Assert.assertEquals( this.jdkDeck.countsBySuit().get(Suit.CLUBS), this.vavrDeck.countsBySuit().get(Suit.CLUBS).get()); }
### Question: VavrDeckOfCardsAsSortedSet { public Map<Rank, Long> countsByRank() { return null; } VavrDeckOfCardsAsSortedSet(); List<Card> shuffle(Random random); Tuple2<Set<Card>, ? extends List<Card>> deal(List<Card> stack, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); SortedSet<Card> getCards(); Map<Suit, ? extends SortedSet<Card>> getCardsBySuit(); }### Answer: @Test public void countsByRank() { Assert.assertEquals( this.jdkDeck.countsByRank().get(Rank.TEN), this.vavrDeck.countsByRank().get(Rank.TEN).get()); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public SortedSet<Card> getCards() { return this.cards; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void allCards() { Assert.assertEquals(this.jdkDeck.getCards(), this.acDeck.getCards()); } @Test public void cardsAreImmutable() { var acCards = this.acDeck.getCards(); Verify.assertThrows( UnsupportedOperationException.class, () -> acCards.remove(null)); Verify.assertThrows( UnsupportedOperationException.class, acCards::clear); Verify.assertThrows( UnsupportedOperationException.class, () -> acCards.add(null)); }
### Question: JDK8DeckOfCardsAsList { public List<Card> diamonds() { return null; } JDK8DeckOfCardsAsList(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands(Deque<Card> shuffled, int hands, int cardsPerHand); List<Card> diamonds(); List<Card> hearts(); List<Card> spades(); List<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); List<Card> getCards(); Map<Suit, List<Card>> getCardsBySuit(); }### Answer: @Test public void diamonds() { Assert.assertEquals(this.jdk1Deck.diamonds(), this.jdk2Deck.diamonds()); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public SortedSet<Card> diamonds() { return null; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void diamonds() { Assert.assertEquals(this.jdkDeck.diamonds(), this.acDeck.diamonds()); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public SortedSet<Card> hearts() { return null; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void hearts() { Assert.assertEquals(this.jdkDeck.hearts(), this.acDeck.hearts()); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public SortedSet<Card> spades() { return null; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void spades() { Assert.assertEquals(this.jdkDeck.spades(), this.acDeck.spades()); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public SortedSet<Card> clubs() { return null; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void clubs() { Assert.assertEquals(this.jdkDeck.clubs(), this.acDeck.clubs()); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public Set<Card> deal(Deque<Card> deque, int count) { Set<Card> hand = new HashSet<>(); IntStream.range(0, count).forEach(i -> hand.add(deque.pop())); return hand; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void deal() { Deque<Card> jdkShuffle = this.jdkDeck.shuffle(new Random(1)); Deque<Card> acShuffle = this.acDeck.shuffle(new Random(1)); Set<Card> jdkHand = this.jdkDeck.deal(jdkShuffle, 5); Set<Card> acHand = this.acDeck.deal(acShuffle, 5); Assert.assertEquals(jdkHand, acHand); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand) { Deque<Card> shuffled = this.shuffle(random); return this.dealHands(shuffled, hands, cardsPerHand); } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void shuffleAndDealHands() { List<Set<Card>> jdkHands = this.jdkDeck.shuffleAndDeal(new Random(1), 5, 5); List<Set<Card>> acHands = this.acDeck.shuffleAndDeal(new Random(1), 5, 5); Assert.assertEquals(jdkHands, acHands); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand) { return null; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void dealHands() { Deque<Card> jdkShuffled = this.jdkDeck.shuffle(new Random(1)); Deque<Card> acShuffled = this.acDeck.shuffle(new Random(1)); List<Set<Card>> jdkHands = this.jdkDeck.dealHands(jdkShuffled, 5, 5); List<Set<Card>> acHands = this.acDeck.dealHands(acShuffled, 5, 5); Assert.assertEquals(jdkHands, acHands); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public MultiValuedMap<Suit, Card> getCardsBySuit() { return this.cardsBySuit; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void cardsBySuit() { Map<Suit, SortedSet<Card>> jdkCardsBySuit = this.jdkDeck.getCardsBySuit(); MultiValuedMap<Suit, Card> acCardsBySuit = this.acDeck.getCardsBySuit(); Assert.assertEquals(jdkCardsBySuit.get(Suit.CLUBS), new TreeSet<>(acCardsBySuit.get(Suit.CLUBS))); } @Test public void cardsBySuitIsImmutable() { var acCardsBySuit = this.acDeck.getCardsBySuit(); Verify.assertThrows( UnsupportedOperationException.class, () -> acCardsBySuit.remove(Suit.CLUBS)); Verify.assertThrows( UnsupportedOperationException.class, acCardsBySuit::clear); Verify.assertThrows( UnsupportedOperationException.class, () -> acCardsBySuit.get(Suit.CLUBS).remove(null)); Verify.assertThrows( UnsupportedOperationException.class, () -> acCardsBySuit.get(Suit.CLUBS).add(null)); Verify.assertThrows( UnsupportedOperationException.class, acCardsBySuit.get(Suit.CLUBS)::clear); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public Bag<Suit> countsBySuit() { return null; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void countsBySuit() { Assert.assertEquals( this.jdkDeck.countsBySuit().get(Suit.CLUBS).intValue(), this.acDeck.countsBySuit().getCount(Suit.CLUBS)); }
### Question: JDK8DeckOfCardsAsList { public List<Card> hearts() { return null; } JDK8DeckOfCardsAsList(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands(Deque<Card> shuffled, int hands, int cardsPerHand); List<Card> diamonds(); List<Card> hearts(); List<Card> spades(); List<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); List<Card> getCards(); Map<Suit, List<Card>> getCardsBySuit(); }### Answer: @Test public void hearts() { Assert.assertEquals(this.jdk1Deck.hearts(), this.jdk2Deck.hearts()); }
### Question: ApacheCommonsDeckOfCardsAsSortedSet { public MultiSet<Rank> countsByRank() { return null; } ApacheCommonsDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); SortedSet<Card> diamonds(); SortedSet<Card> hearts(); SortedSet<Card> spades(); SortedSet<Card> clubs(); Bag<Suit> countsBySuit(); MultiSet<Rank> countsByRank(); SortedSet<Card> getCards(); MultiValuedMap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void countsByRank() { Assert.assertEquals( this.jdkDeck.countsByRank().get(Rank.TEN).intValue(), this.acDeck.countsByRank().getCount(Rank.EIGHT)); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public ImmutableSortedSet<Card> getCards() { return this.cards; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void allCards() { Assert.assertEquals(this.jdkDeck.getCards(), this.ggDeck.getCards()); } @Test public void cardsAreImmutable() { var ggCards = this.ggDeck.getCards(); Verify.assertThrows( UnsupportedOperationException.class, () -> ggCards.remove(null)); Verify.assertThrows( UnsupportedOperationException.class, ggCards::clear); Verify.assertThrows( UnsupportedOperationException.class, () -> ggCards.add(null)); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public Set<Card> diamonds() { return null; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void diamonds() { Assert.assertEquals(this.jdkDeck.diamonds(), this.ggDeck.diamonds()); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public Set<Card> hearts() { return null; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void hearts() { Assert.assertEquals(this.jdkDeck.hearts(), this.ggDeck.hearts()); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public Set<Card> spades() { return null; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void spades() { Assert.assertEquals(this.jdkDeck.spades(), this.ggDeck.spades()); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public Set<Card> clubs() { return null; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void clubs() { Assert.assertEquals(this.jdkDeck.clubs(), this.ggDeck.clubs()); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public Set<Card> deal(Deque<Card> deque, int count) { Set<Card> hand = new HashSet<>(); IntStream.range(0, count).forEach(i -> hand.add(deque.pop())); return hand; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void deal() { Deque<Card> jdkShuffle = this.jdkDeck.shuffle(new Random(1)); Deque<Card> ggShuffle = this.ggDeck.shuffle(new Random(1)); Set<Card> jdkHand = this.jdkDeck.deal(jdkShuffle, 5); Set<Card> ggHand = this.ggDeck.deal(ggShuffle, 5); Assert.assertEquals(jdkHand, ggHand); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand) { Deque<Card> shuffled = this.shuffle(random); return this.dealHands(shuffled, hands, cardsPerHand); } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void shuffleAndDealHands() { List<Set<Card>> jdkHands = this.jdkDeck.shuffleAndDeal(new Random(1), 5, 5); List<Set<Card>> ggHands = this.ggDeck.shuffleAndDeal(new Random(1), 5, 5); Assert.assertEquals(jdkHands, ggHands); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand) { return null; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void dealHands() { Deque<Card> jdkShuffled = this.jdkDeck.shuffle(new Random(1)); Deque<Card> ggShuffled = this.ggDeck.shuffle(new Random(1)); List<Set<Card>> jdkHands = this.jdkDeck.dealHands(jdkShuffled, 5, 5); List<Set<Card>> ggHands = this.ggDeck.dealHands(ggShuffled, 5, 5); Assert.assertEquals(jdkHands, ggHands); }
### Question: JDK8DeckOfCardsAsList { public List<Card> spades() { return null; } JDK8DeckOfCardsAsList(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); List<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); List<Set<Card>> dealHands(Deque<Card> shuffled, int hands, int cardsPerHand); List<Card> diamonds(); List<Card> hearts(); List<Card> spades(); List<Card> clubs(); Map<Suit, Long> countsBySuit(); Map<Rank, Long> countsByRank(); List<Card> getCards(); Map<Suit, List<Card>> getCardsBySuit(); }### Answer: @Test public void spades() { Assert.assertEquals(this.jdk1Deck.spades(), this.jdk2Deck.spades()); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public ImmutableSetMultimap<Suit, Card> getCardsBySuit() { return this.cardsBySuit; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void cardsBySuit() { Map<Suit, SortedSet<Card>> jdkCardsBySuit = this.jdkDeck.getCardsBySuit(); ImmutableSetMultimap<Suit, Card> ggCardsBySuit = this.ggDeck.getCardsBySuit(); Assert.assertEquals(jdkCardsBySuit.get(Suit.CLUBS), ggCardsBySuit.get(Suit.CLUBS)); } @Test public void cardsBySuitIsImmutable() { var ggCardsBySuit = this.ggDeck.getCardsBySuit(); Verify.assertThrows( UnsupportedOperationException.class, () -> ggCardsBySuit.removeAll(Suit.CLUBS)); Verify.assertThrows( UnsupportedOperationException.class, ggCardsBySuit::clear); Verify.assertThrows( UnsupportedOperationException.class, () -> ggCardsBySuit.get(Suit.CLUBS).remove(null)); Verify.assertThrows( UnsupportedOperationException.class, () -> ggCardsBySuit.get(Suit.CLUBS).add(null)); Verify.assertThrows( UnsupportedOperationException.class, ggCardsBySuit.get(Suit.CLUBS)::clear); }
### Question: GoogleGuavaDeckOfCardsAsSortedSet { public Multiset<Suit> countsBySuit() { return null; } GoogleGuavaDeckOfCardsAsSortedSet(); Deque<Card> shuffle(Random random); Set<Card> deal(Deque<Card> deque, int count); ImmutableList<Set<Card>> shuffleAndDeal(Random random, int hands, int cardsPerHand); ImmutableList<Set<Card>> dealHands( Deque<Card> shuffled, int hands, int cardsPerHand); Set<Card> diamonds(); Set<Card> hearts(); Set<Card> spades(); Set<Card> clubs(); Multiset<Suit> countsBySuit(); Multiset<Rank> countsByRank(); ImmutableSortedSet<Card> getCards(); ImmutableSetMultimap<Suit, Card> getCardsBySuit(); }### Answer: @Test public void countsBySuit() { Assert.assertEquals( this.jdkDeck.countsBySuit().get(Suit.CLUBS).intValue(), this.ggDeck.countsBySuit().count(Suit.CLUBS)); }