1 package com.github.davidmoten.rx.buffertofile;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import com.github.davidmoten.util.Preconditions;
7
8 import rx.functions.Func0;
9
10 public final class Options {
11
12 public static final String DEFAULT_FILE_PREFIX = "bufferToFileDb";
13
14 private final Func0<File> fileFactory;
15 private final boolean delayError;
16 private final long rolloverEvery;
17 private final long rolloverSizeBytes;
18 private final int bufferSizeBytes;
19
20 private Options(Func0<File> filefactory, boolean delayError, long rolloverEvery, int bufferSizeBytes,
21 long rolloverSizeBytes) {
22 Preconditions.checkNotNull(filefactory);
23 Preconditions.checkArgument(rolloverSizeBytes > 0, "rolloverSizeBytes must be greater than zero");
24 Preconditions.checkArgument(rolloverEvery > 1, "rolloverEvery must be greater than one");
25 Preconditions.checkArgument(bufferSizeBytes > 0, "bufferSizeBytes must be greater than 0");
26 this.fileFactory = filefactory;
27 this.delayError = delayError;
28 this.rolloverEvery = rolloverEvery;
29 this.rolloverSizeBytes = rolloverSizeBytes;
30 this.bufferSizeBytes = bufferSizeBytes;
31 }
32
33 public Func0<File> fileFactory() {
34 return fileFactory;
35 }
36
37 public boolean delayError() {
38 return delayError;
39 }
40
41 public long rolloverEvery() {
42 return rolloverEvery;
43 }
44
45 public long rolloverSizeBytes() {
46 return rolloverSizeBytes;
47 }
48
49 public int bufferSizeBytes() {
50 return bufferSizeBytes;
51 }
52
53 public boolean rolloverEnabled() {
54 return rolloverSizeBytes != Long.MAX_VALUE || rolloverEvery != Long.MAX_VALUE;
55 }
56
57
58
59
60
61
62
63
64 private static Builder builder() {
65 return new Builder();
66 }
67
68 public static Builder fileFactory(Func0<File> fileFactory) {
69 return builder().fileFactory(fileFactory);
70 }
71
72 public static Builder delayError(boolean delayError) {
73 return builder().delayError(delayError);
74 }
75
76 public static Builder rolloverEvery(long rolloverEvery) {
77 return builder().rolloverEvery(rolloverEvery);
78 }
79
80 public static Builder rolloverSizeBytes(long rolloverSizeBytes) {
81 return builder().rolloverSizeBytes(rolloverSizeBytes);
82 }
83
84 public static Builder rolloverSizeMB(double rolloverSizeMB) {
85 return builder().rolloverSizeMB(rolloverSizeMB);
86 }
87
88 public static Builder disableRollover() {
89 return builder().disableRollover();
90 }
91
92 public static Builder bufferSizeBytes(int bufferSizeBytes) {
93 return builder().bufferSizeBytes(bufferSizeBytes);
94 }
95
96 public static Options defaultInstance() {
97 return builder().build();
98 }
99
100 public static class Builder {
101
102 private Func0<File> fileFactory = FileFactoryHolder.INSTANCE;
103 private boolean delayError = true;
104 private long rolloverSizeBytes = Long.MAX_VALUE;
105 private long rolloverEvery = 1000000;
106 private int bufferSizeBytes = 1024;
107
108 private Builder() {
109 }
110
111 public Builder rolloverSizeMB(double rolloverSizeMB) {
112 return rolloverSizeBytes(Math.round(rolloverSizeMB * 1024 * 1024));
113 }
114
115 public Builder rolloverSizeBytes(long rolloverSizeBytes) {
116 this.rolloverSizeBytes = rolloverSizeBytes;
117 return this;
118 }
119
120 public Builder disableRollover() {
121 this.rolloverSizeBytes = Long.MAX_VALUE;
122 return rolloverEvery(Long.MAX_VALUE);
123 }
124
125 public Builder bufferSizeBytes(int bufferSizeBytes) {
126 this.bufferSizeBytes = bufferSizeBytes;
127 return this;
128 }
129
130 public Builder rolloverEvery(long rolloverEvery) {
131 this.rolloverEvery = rolloverEvery;
132 return this;
133 }
134
135
136
137
138
139
140
141
142
143
144 public Builder fileFactory(Func0<File> fileFactory) {
145 this.fileFactory = fileFactory;
146 return this;
147 }
148
149
150
151
152
153
154
155
156
157 public Builder delayError(boolean delayError) {
158 this.delayError = delayError;
159 return this;
160 }
161
162 public Options build() {
163 return new Options(fileFactory, delayError, rolloverEvery, bufferSizeBytes, rolloverSizeBytes);
164 }
165 }
166
167 private static class FileFactoryHolder {
168
169 private static final Func0<File> INSTANCE = new Func0<File>() {
170 @Override
171 public File call() {
172 try {
173 return File.createTempFile(DEFAULT_FILE_PREFIX, "");
174 } catch (IOException e) {
175 throw new RuntimeException(e);
176 }
177 }
178 };
179 }
180
181 }