guava Joiner和MapJoiner线程安全

  • {@code Joiner} 和 {@code MapJoiner} 的实例总是 immutable(详见java并发编程实战相关部分)

  • [ 1.状态创建后不能被修改. 2.所有的域都是final类型,并且它被正确创建(创建期间没有发生this引用的逸出) ]

  • 创建通过静态函数on,内部调用构造函数,对于设置null类型的时候,函数内部会生成一个匿名类,从而重载设置null类型的相关函数.

  • 所以他们都是线程安全的. 他们的构造函数都是私有的,必须调用他们的静态方法生成他们的对象(不用反射构造对象情况下)

    
     public class Chapter2Code {
    
    	private final List<String> list = Lists.newArrayList("name", "doctor", "sex", "man", null);
    
    	@Rule
    	public ExpectedException expectedException = ExpectedException.none();
    
    	@Test
    	public void test_Joiner() {
    
    		Joiner joiner = Joiner.on(",").skipNulls();
    
    		assertThat(joiner.join(list), equalTo("name,doctor,sex,man"));
    
    		joiner = Joiner.on(",").useForNull("null");
    		assertThat(joiner.join(list), equalTo("name,doctor,sex,man,null"));
    
    		joiner = Joiner.on("|");
    		expectedException.expect(NullPointerException.class);// 如果不处理空指针,会抛出异常.
    		joiner.join(list);
    
    	}
    
    	@Test
    	public void test_Joiner_wrong_use() {
    
    		Joiner joiner = Joiner.on(",");
    		joiner.skipNulls();// 这个方法会生成一个新的对象,对原对象的设置无影响
    		expectedException.expect(NullPointerException.class);
    		joiner.join(list);
    	}
    
    	@Test
    	public void test_Joiner_appendTo_StringBuilder() {
    		Joiner joiner = Joiner.on("|").skipNulls();
    		StringBuilder builder = new StringBuilder(256);
    
    		joiner.appendTo(builder, list);
    
    		assertThat(builder.toString(), equalTo("name|doctor|sex|man"));
    		System.out.println(builder.toString());
    	}
    
    	@Test
    	public void test_Joiner_appendTo_File() throws IOException {
    		File file = new File("a.txt");
    
    		if (!file.exists()) {
    			file.createNewFile();
    		}
    
    		try (FileWriter fileWriter = new FileWriter(file);) {
    			Joiner.on("#").skipNulls().appendTo(fileWriter, list);
    
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    
    		List<String> lines = Files.readAllLines(Paths.get("a.txt"));
    
    		System.out.println(lines);
    		assertThat(Joiner.on("").join(lines), equalTo(Joiner.on("#").skipNulls().join(list)));
    
    	}
    
    	@Test
    	public void test_MapJoiner() {
    		Map<String, String> map = Maps.newLinkedHashMap();
    		map.put("name", "doctor");
    		map.put("sex", "man");
    
    		String join = Joiner.on(",").withKeyValueSeparator("=").join(map);
    		assertThat(join, equalTo("name=doctor,sex=man"));
    	}
    

}

转载于:https://my.oschina.net/doctor2014/blog/387078

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值