]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/Preferences.java
Limit SCL Console buffer size to 5M characters by default
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / console / Preferences.java
1 package org.simantics.scl.ui.console;
2
3 import java.util.Collection;
4 import java.util.Deque;
5 import java.util.Iterator;
6 import java.util.LinkedList;
7 import java.util.Set;
8 import java.util.TreeSet;
9
10 /**
11  * @author Antti Villberg
12  */
13 public final class Preferences {
14
15     /**
16      * Console buffer high and low water marks
17      */
18     public static final String CONSOLE_LIMIT_CONSOLE_OUTPUT = "SCLConsole.limitConsoleOutput";       //$NON-NLS-1$
19
20     public static final String CONSOLE_LOW_WATER_MARK       = "SCLConsole.lowWaterMark";             //$NON-NLS-1$
21
22     public static final String CONSOLE_HIGH_WATER_MARK      = "SCLConsole.highWaterMark";            //$NON-NLS-1$
23
24     /**
25      * By default console output buffer size is limited.
26      */
27     public static final boolean CONSOLE_LIMIT_CONSOLE_OUTPUT_DEFAULT = true;
28
29     /**
30      * The console low water mark default value {@value #CONSOLE_LOW_WATER_MARK_DEFAULT_VALUE}.
31      */
32     public static final int CONSOLE_LOW_WATER_MARK_DEFAULT_VALUE = 5000000;
33
34     /**
35      * The console low water mark default value {@value #CONSOLE_HIGH_WATER_MARK_DEFAULT_VALUE}.
36      */
37     public static final int CONSOLE_HIGH_WATER_MARK_DEFAULT_VALUE = highWatermarkForLow(CONSOLE_LOW_WATER_MARK_DEFAULT_VALUE);
38
39     /**
40      * The console low water mark maximum value {@value #CONSOLE_LOW_WATER_MARK_MAX_VALUE}.
41      */
42     public static final int CONSOLE_LOW_WATER_MARK_MAX_VALUE = 10000000;
43
44     public static int highWatermarkForLow(int low) {
45         return low + 80*100;
46     }
47
48     public static final String  COMMAND_HISTORY = "COMMAND_HISTORY"; //$NON-NLS-1$
49
50     private static final String DELIMITER       = "¤¤¤¤"; //$NON-NLS-1$
51
52     public static String join(Collection<String> s, String delimiter) {
53         if (s == null || s.isEmpty()) return ""; //$NON-NLS-1$
54         Iterator<String> iter = s.iterator();
55         StringBuilder builder = new StringBuilder(iter.next());
56         while( iter.hasNext() )
57         {
58             builder.append(delimiter).append(iter.next());
59         }
60         return builder.toString();
61     }
62     
63     public static Deque<String> decodePaths(String recentPathsPref) {
64         Deque<String> result = new LinkedList<String>();
65         for(String s : recentPathsPref.split(DELIMITER)) result.add(s);
66         return result;
67     }
68
69     public static String encodePaths(Collection<String> recentPaths) {
70         return join(recentPaths, DELIMITER);
71     }
72
73     public static <T> void removeDuplicates(Iterable<String> iter) {
74         // Remove duplicates
75         Set<String> dups = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
76         for (Iterator<String> it = iter.iterator(); it.hasNext();) {
77             String path = it.next();
78             if (!dups.add(path)) {
79                 it.remove();
80             }
81         }
82     }
83
84 }