wzy
2021-01-11 1f4ac41109041ef9e2eb163cc03123e3a267650b
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.matrix.component.ueditor.define;
 
import com.matrix.component.ueditor.Encoder;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
/**
 * 多状态集合状态
 * 其包含了多个状态的集合, 其本身自己也是一个状态
 * @author hancong03@baidu.com
 *
 */
public class MultiState implements State {
 
    private boolean state = false;
    private String info = null;
    private Map<String, Long> intMap = new HashMap<String, Long>();
    private Map<String, String> infoMap = new HashMap<String, String>();
    private List<String> stateList = new ArrayList<String>();
    
    public MultiState ( boolean state ) {
        this.state = state;
    }
    
    public MultiState ( boolean state, String info ) {
        this.state = state;
        this.info = info;
    }
    
    public MultiState ( boolean state, int infoKey ) {
        this.state = state;
        this.info = AppInfo.getStateInfo( infoKey );
    }
    
    @Override
    public boolean isSuccess() {
        return this.state;
    }
    
    public void addState ( State state ) {
        stateList.add( state.toJSONString() );
    }
 
    /**
     * 该方法调用无效果
     */
    @Override
    public void putInfo(String name, String val) {
        this.infoMap.put(name, val);
    }
 
    @Override
    public String toJSONString() {
        
        String stateVal = this.isSuccess() ? AppInfo.getStateInfo( AppInfo.SUCCESS ) : this.info;
        
        StringBuilder builder = new StringBuilder();
        
        builder.append( "{\"state\": \"" + stateVal + "\"" );
        
        // 数字转换
        Iterator<String> iterator = this.intMap.keySet().iterator();
        
        while ( iterator.hasNext() ) {
            
            stateVal = iterator.next();
            
            builder.append( ",\""+ stateVal +"\": " + this.intMap.get( stateVal ) );
            
        }
        
        iterator = this.infoMap.keySet().iterator();
        
        while ( iterator.hasNext() ) {
            
            stateVal = iterator.next();
            
            builder.append( ",\""+ stateVal +"\": \"" + this.infoMap.get( stateVal ) + "\"" );
            
        }
        
        builder.append( ", list: [" );
        
        
        iterator = this.stateList.iterator();
        
        while ( iterator.hasNext() ) {
            
            builder.append( iterator.next() + "," );
            
        }
        
        if ( this.stateList.size() > 0 ) {
            builder.deleteCharAt( builder.length() - 1 );
        }
        
        builder.append( " ]}" );
 
        return Encoder.toUnicode( builder.toString() );
 
    }
 
    @Override
    public void putInfo(String name, long val) {
        this.intMap.put( name, val );
    }
 
}