Skip to content

Instantly share code, notes, and snippets.

@xingstarx
xingstarx / Properties.kt
Last active January 15, 2018 10:31 — forked from pardom-zz/Properties.kt
Kotlin observable properties
/**
** 为KProperty0添加一个扩展函数addObserver(),用来监听当值发生变化的时候刷新observer代码块
**/
fun <T> KProperty0<T>.addObserver(observer: (T) -> Unit) {
(getDelegate(this) as ObservableProperty<T>?)?.addObserver(observer)
}
private fun <T> getDelegate(kProperty: KProperty0<T>) : Any? {
val owner = (kProperty as PropertyReference).owner
@xingstarx
xingstarx / BigDecimalExtendsion.md
Created January 5, 2018 07:20
Koltin 扩展sumByBigDecimal方法

Iterable提供了一个sumByDouble方法,但是对于BigDecimal类型的Iterable运算的结果实在是差强人意,看下面这段代码的运行结果:

var list = listOf<BigDecimal>(BigDecimal(10.21.toString()), BigDecimal(8.21.toString()), BigDecimal(12.21.toString()))
    var repaymentAmount = BigDecimal(list.sumByDouble { it.toDouble() })
//    var repaymentAmount = list.sumByBigDecimal { it }
    println("repaymentAmount == $repaymentAmount")

输出的是repaymentAmount == 30.63000000000000255795384873636066913604736328125

仿照sumByDouble方法扩展了一个新的方法sumByBigDecimal,做了部分修改,具体如下

Image source

https://gyazo.com/eb5c5741b6a9a16c692170a41a49c858.png

Try resize it!

  • ![](https://gyazo.com/eb5c5741b6a9a16c692170a41a49c858.png | width=100)
@xingstarx
xingstarx / mac上mysql启动报错.md
Created November 27, 2017 10:08
mac上mysql启动报错 Can't connect to local MySQL server through socket '/tmp/mysql.sock'
  1. mysql 报错:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

使用brew install mysql 安装MySQL后,有时候会出现这个问题,是由于mysql的服务还没有启动。 需要执行mysql.server start 命令就可以了

@xingstarx
xingstarx / KeyBoardChangeListener.java
Created September 12, 2017 03:59
监听软键盘是否开启或者关闭
/**
* https://pspdfkit.com/blog/2016/keyboard-handling-on-android/
* 监听软键盘变化(针对full screen model)
*/
public class KeyBoardChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
private static final String TAG = "KeyBoardChangeListener";
// Threshold for minimal keyboard height.
final int MIN_KEYBOARD_HEIGHT_PX = 150;
private final Rect windowVisibleDisplayFrame = new Rect();
// Top-level window decor view.
@xingstarx
xingstarx / default.java
Created August 25, 2017 14:04 — forked from binjoo/default.java
JAVA:清除HTML标签
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTMLSpirit{
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
@xingstarx
xingstarx / MainActivity.java
Created August 14, 2017 13:35 — forked from nickbutcher/MainActivity.java
A quick sample of the new physics-based animation library added in Support Library 25.3.0 docs: https://developer.android.com/reference/android/support/animation/package-summary.html output: https://twitter.com/crafty/status/842055117323026432
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@xingstarx
xingstarx / MapDeserializerDoubleAsIntFix.java
Last active March 21, 2022 02:21
处理Gson中,json转换map造成的int变double的问题
/**
*最近在研究网络请求数据解析的问题,发现json数据被强制转换为map结构的时候,会出现int变成double的问题
*在stackoverflow上看到了一个这个How can I prevent gson from converting integers to doubles 的问题,采用了这个答案
*https://stackoverflow.com/a/36529534/5279354答案
*/
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
@xingstarx
xingstarx / JSONUtil.java
Created August 4, 2017 10:25 — forked from jjfiv/JSONUtil.java
JSON escaping and unescaping that really works, no dependencies.
// BSD License (http://lemurproject.org/galago-license)
package org.lemurproject.galago.utility.json;
public class JSONUtil {
public static String escape(String input) {
StringBuilder output = new StringBuilder();
for(int i=0; i<input.length(); i++) {
char ch = input.charAt(i);
int chx = (int) ch;
@xingstarx
xingstarx / StringEscapeUtilsTest.java
Created July 25, 2017 06:52
java 中json转义处理
/**
* 将一个json串拍平为纯粹的字符串,也就是说,将json串序列化为string的过程中保留双引号这样的,保留转义字符
*/
@Test
public void test2() {
String str = "[{\"image\"=\"https://pic4.zhimg.com/v2-01d1919be61d42c9ff2e4b82e6e55f1b.jpg\",\"type\"=0, \"id\"=9540230,\"ga_prefix\"=072507, \"title\"=\"日本新推出的「减肥可乐」,喝了真能减肥吗\"}, " +
"{\"image\"=\"https://pic2.zhimg.com/v2-9caa37a6eba40ce688f25d88dc1d747d.jpg\", \"type\"=0, \"id\"=9539691, \"ga_prefix\"=072507, \"title\"=\"为了演好崇祯皇帝朱由检,和金士杰对戏前我好几天没吃饭\"}, " +
"{\"image\"=\"https://pic2.zhimg.com/v2-34d7bfd12dfc8e3e73ee1450eb8ede35.jpg\", \"type\"=0, \"id\"=9539642, \"ga_prefix\"=072511, \"title\"=\"安徽小岗村第一个包产到户,为什么现在还是那么穷?\"}, " +