Skip to content

Instantly share code, notes, and snippets.

@americanstone
americanstone / gist:5c42416011ae64b7df340d080c521d67
Last active November 22, 2021 23:16
copy a repro & make code change & push to different repro
clone the project
git remote rename origin upstream
git remote add origin <your own project>
use git desktop push upstream
`upstream` for fetch & pull
`origin` for push
@americanstone
americanstone / .txt
Created February 8, 2021 02:44
differences-between-proxy-and-decorator-pattern!!!
https://stackoverflow.com/questions/18618779/differences-between-proxy-and-decorator-pattern
@americanstone
americanstone / .gitignore
Last active October 13, 2020 01:41
.gitignore
### Local Configuration Files ###
fookeeper-local.json
### System Files ###
.DS_Store
### Managed Files ###
target/
.terraform/
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create new ConcurrentHashMap and put 1000 entries to it, with keys=1,2,3,...,1000 and all values=0.
2. Start 2 parallel threads:
- First thread: for i=1,2,3,...,1000 puts an entry (k=i, v=1) into the map;
- Second thread: removes all `0` from the map: `map.entrySet().removeIf(e -> e.getValue() == 0)`.
3. Check size of the map.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
@americanstone
americanstone / gist:c1ef03e6fbf61cc30a1b73d8952ae907
Created November 28, 2018 21:08
get content from resource folder
private String getFileWithUtil(String fileName) {
String result = "";
ClassLoader classLoader = getClass().getClassLoader();
try {
result = IOUtils.toString(classLoader.getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
}
@americanstone
americanstone / 5way.txt
Created August 28, 2018 03:20
5 ways to customize Spring MVC JSON/XML output
To adhere to guidelines or requirements, API designers may want to control how JSON/XML responses are formatted. Spring Web makes use of Jackson to perform JSON/XML serialization. Therefore, to customize our output format, we must configure the Jackson processor. Spring Web offers XML-based or Java-based approaches to handling configuration. In this article, we will look at the Java-based configuration.
Enable XML output
First of all, if you have created your project through https://start.spring.io/ you need add the following dependency to your project because there is no XML serialization by default:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
ok, now we can investigate the output customization process. Imagine we have this REST controller:
@americanstone
americanstone / recursivelyCompareDirectories.java
Created February 19, 2018 14:56
recursivelyCompareDirectories
/**
* Recursively compare the contents of the directory of downloaded
* files with the contents of the "ground truth" directory.
*/
public static void recursivelyCompareDirectories(File expected,
File generated)
throws IOException {
// Checks parameters.
assertTrue("Generated Folder doesn't exist: " + generated,
generated.exists());
@americanstone
americanstone / RestROFactory.java
Last active February 19, 2018 14:51
Better way to construct REST request params?
package com.dealer.modules.cms.promotions.factory;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
@americanstone
americanstone / Device.java
Created February 19, 2018 14:35
builder pattern with singleton
package edu.vanderbilt.platform;
import java.util.concurrent.CancellationException;
import edu.vanderbilt.utils.Crawler;
import edu.vanderbilt.utils.Options;
/**
* A container singleton class that provides access to the platform
* dependent objects Platform, Options, and Crawler. This singleton
@americanstone
americanstone / Options.java
Created February 19, 2018 14:35
builder pattern with default
package edu.vanderbilt.utils;
import edu.vanderbilt.platform.PlatformOptions;
/**
* Immutable data class containing all crawling options.
*/
public class Options implements PlatformOptions {
public static String DEFAULT_WEB_URL = "http://www.dre.vanderbilt.edu/~schmidt/imgs";
public static String DEFAULT_DOWNLOAD_DIR_NAME = "downloaded-images";