Skip to content

Instantly share code, notes, and snippets.

View RealDeanZhao's full-sized avatar
🤒
Out sick

Dean Zhao RealDeanZhao

🤒
Out sick
View GitHub Profile
@RealDeanZhao
RealDeanZhao / lazily-initialized-hash-code.md
Created March 28, 2017 13:52
JAVA Lazily initialized, cached hashCode

If a class is immutable and the cost of computing the hash code is significant, you might consider caching the hash code in the object rather than recalculating it each time it is requested. If you believe that most objects of this type will be used as hash keys, then you should calculate the hash code when the instance is created. Otherwise, you might choose to lazily initialize it the first time hashCode is invoked (Item 71).

// Lazily initialized, cached hashCode
private volatile int hashCode;  // (See Item 71)
@Override public int hashCode() {
       int result = hashCode;
       if (result == 0) {
           result = 17;
           result = 31 * result + areaCode;
 result = 31 * result + prefix;
@RealDeanZhao
RealDeanZhao / enum-singleton.java
Created March 27, 2017 05:19
a singleton using enum
```
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}
static void main(){
Elvis.INSTANCE.leaveTheBuilding();
}
```
@RealDeanZhao
RealDeanZhao / redux-orm-react-act.js
Created March 22, 2017 07:14
redux-orm customized actionHandler integrated with react-act
// In the model class
static actionHandler() {
return {
[documentAction.receiveDocumentList]: (state, payload) => {
const session = orm.session(state || state.getEmptyState());
const { DocumentModel } = session;
DocumentModel.all().toModelArray().forEach(model => {
model.update({ showInList: false, selected: false });
});
payload.forEach(document => {
@RealDeanZhao
RealDeanZhao / redux-observable-epic-with-concat.js
Created March 22, 2017 07:11
redux-observable-epic-biolerplate-with-concat
export const settlementEpic = {
fetchDocumentList: (action$) => {
return action$.ofType(settlementAction.fetchDocumentList.getType())
.map(action => action.payload)
.switchMap(payload => {
const userId = window.localStorage.getItem('userid');
let path = `/api/v1/documents?ownerId=${userId}`;
if (payload && payload.query && payload.query.settlementBuyer) {
path = path.concat(`&buyer=${payload.query.settlementBuyer}`);
}
@RealDeanZhao
RealDeanZhao / lambdaAndOptional.java
Created March 22, 2017 07:08
Deal with Optional using lambda
if (...) {
return redirectToAuthCodeUrl();
} else {
return Response.ok().build();
}
-->
return sessionToken
.filter(it -> ...)
@RealDeanZhao
RealDeanZhao / redux form unfocus when input the first charactor
Last active August 23, 2016 05:45
redux form unfocus when input the first charactor
// https://github.com/erikras/redux-form/releases/tag/v6.0.0-alpha.14
Let's look at what that means:
WRONG: :cry: :-1:
class MyForm extends Component {
render() {
const { handleSubmit } = this.props
refs: {
[string: string]: any;
textarea: any; // this should be the same as the html tag
selectedTabTitle: any;
}
<textarea > </textarea>
// a Form
class FormBase extends React.Component<any, {}>{
render() {
return (
<form onSubmit={this.props.handleSubmit}>
<div>
<label>Username</label>
<Field component='input' className="form-control" type="text" placeholder="First Name" name='username'/>
</div>
</form>
class ClassBase extends React.Component<any, any>{
componentWillMount = function (): any {
const {topicList, dispatch} = this.props;
dispatch(fetchTopics());
};
render() {
return (
<div></div>
);
router.get('/api/v1/auth/github/callback',
return passport.authenticate('github', {sucessRedirect: '/'});
);
==>
router.get('/api/v1/auth/github/callback',
async (ctx: any, next: any) => {
return passport.authenticate('github', (user: any, info: any, status: any) => {
ctx.redirect('/');
})(ctx, next);
}