Java Stream – Map and FlatMap

Here is an example from book, it converts list of string into list of chars without duplicated. it is good example to show what different between map and flatmap. basically map will keep original structure of input element while flatmap won’t keep structure of input element.

Mockito – ByteBuddy Examples

ByteBuddy is key framework used in Mockito, it will auto generated subclass and override default methods. Following code will print out “Hello ByteBuddy!”.

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;

public class ByteBuddyEntry {
    public static void main(String[] args)
    {
        DynamicType.Unloaded unloadedType = new ByteBuddy()
                .subclass(Object.class)
                .method(ElementMatchers.isToString())
                .intercept(FixedValue.value("Hello ByteBuddy!"))
                .make();

        Class<?> dynamicType = unloadedType.load(ByteBuddyEntry.class
                        .getClassLoader())
                .getLoaded();

        try {
            System.out.println(dynamicType.newInstance().toString());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

MySQL Core Src – Lex/Parse SQL Statement

SQL lex files:

sql/sql_lex.h、sql/lex_token.h、sql/lex.h、sql/lex_symbol.h

sql/gen_lex_token.cc、sql/sql_lex.cc

SQL grammar parser file:

sql/sql_yacc.yy、sql/sql_yacc.cc、sql/sql_yacc.h

SQL hint grammar parser file:

sql/sql_hints.yy、sql/sql_hints.yy.cc

mysql use bison (lex/yacc) to token sql statement and parse it, take sql_yacc.yy for example, it is main file contains all parse sql rules.

Docker – Setup git ssh server using docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
FROM ubuntu:18.04
 
ENV TZ=Australia/Sydney
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
 
RUN apt-get update
RUN apt-get install -y bash git python3 g++ curl vim openssh-server sudo
 
 
RUN addgroup --gid 9999 git && adduser --uid 9999 --gid 9999 --shell /usr/bin/git-shell git
RUN addgroup --gid 9000 xxx && adduser --uid 9000 --gid 9000 --shell /bin/bash xxx
 
RUN echo git:password | chpasswd
RUN echo xxx:password | chpasswd
 
RUN mkdir -p /home/git
RUN mkdir -p /home/xxx
 
RUN chown -Rh git:git /home/git
 
COPY sshd_config /etc/ssh/sshd_config
 
EXPOSE 22
 
ENTRYPOINT service ssh restart && bash
 
CMD ["/bin/bash"]

1863. Sum of All Subset XOR Totals

key idea is how to generate all subset of array.

take 3 elements of arrays for example,

[a, _, _] -> [1, 0, 0]
[_, b, _] -> [0, 1, 0]
[a, b, _] -> [1, 1, 0]
[_, _, c] -> [0, 0, 1]
[a, _, c] -> [1, 0, 1]
[_, b, c] -> [0, 1, 1]
[a, b, c] -> [1, 1, 1]

integer i is loop through 1 to Math.pow(2, nums.length) – 1, for each integer, we check how many 1 bit

i & 1, if it is 1, we get nums[index] out as elements for subarray.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
class Solution {
    public int subsetXORSum(int[] nums) {
        int result = 0;
    	int x = (int)Math.pow(2, nums.length) - 1;
        for(int i=1;i<=x;i++) {
        	int p = i;
        	int r = 0;
        	for(int j=0;j<nums.length;j++) {
        		int t = p & 1;
        		p >>= 1;
 
        		if (t == 1) {
    				r ^= nums[j];
        		}
        	}
 
        	result += r;
        }
 
        return result;
    }
}

Git – How to sync with two remote repos using git

git remote -v
git remote
git remote add pb https://github.com/paulboone/ticgit

$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
pb https://github.com/paulboone/ticgit (fetch)
pb https://github.com/paulboone/ticgit (push)

$ git fetch pb
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
* [new branch] master -> pb/master
* [new branch] ticgit -> pb/ticgit

$ git checkout master

$ git merge upstream/master # important, merge fetch code and master

$ git push

java – Spring Mergeable

There are 4 type of Mergeable Object in Spring

ManagedList
ManagedProperties
ManagedMap
ManagedSet

Take ManagedList as example

if mergeEnabled is set to be false, merge will stop
if parent is null, return itself.
and finally it will add parent firstly and then add children.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	@Override
	@SuppressWarnings("unchecked")
	public List<E> merge(@Nullable Object parent)
	{
		if (!this.mergeEnabled) {
			throw new IllegalStateException("Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
		}
		if (parent == null) {
			return this;
		}
		if (!(parent instanceof List)) {
			throw new IllegalArgumentException("Cannot merge with object of type [" + parent.getClass() + "]");
		}
		List<E> merged = new ManagedList<>();
		merged.addAll((List<E>) parent);
		merged.addAll(this);
		return merged;
	}

java – method bridge

java reflection could get all methods of a class.

isBridge() could check whether method is bridged or not.

when we override parent class general type method in child class, method will become bridged.

here is example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package org.spring.main;
 
import java.lang.reflect.Method;
 
import org.springframework.core.BridgeMethodResolver;
 
public class BridgeMethodResolverEntry {
 
	public class Node<T> {
 
	    public T data;
 
	    public Node(T data) { this.data = data; }
 
	    public void setData(T data) {
	        System.out.println("Node.setData");
	        this.data = data;
	    }
	}
 
	public class MyNode extends Node<Integer> {
 
	    public MyNode(Integer data) { super(data); }
 
	    @Override
	    public void setData(Integer data) {
	        System.out.println("MyNode.setData");
	        super.setData(data);
	    }
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BridgeMethodResolverEntry x = new BridgeMethodResolverEntry();
		MyNode resolver = x.new MyNode(5);
		Method[] declaredMethods = resolver.getClass().getDeclaredMethods();
 
		for (int i = 0; i < declaredMethods.length; i++) {
		            Method declaredMethod = declaredMethods[i];
		            String methodName = declaredMethod.getName();
		            Class<?> returnType = declaredMethod.getReturnType();
		            Class<?> declaringClass = declaredMethod.getDeclaringClass();
		            boolean bridge = declaredMethod.isBridge();
		            System.out.print((i+1) + " method name is" + methodName + ", return type is " + returnType + "  ");
		            System.out.print(bridge ? " is Bridge Method" : " is not Bridge Method");
		            System.out.println("  it is declared at "+declaringClass.getSimpleName()+"");            
		}                        
 
	}
 
}

curl as postman cli

curl could be used as POSTMan CLI.

for PDF downloading restful API, curl actions better that postman

1
curl -v -X GET -u username:password https://<url>