MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / hook

Method hook

src/main/java/field/utility/AutoPersist.java:127–152  ·  view source on GitHub ↗
(String name, T x, Supplier<T> sx)

Source from the content-addressed store, hash-verified

125 }
126
127 private static <T extends Serializable> T hook(String name, T x, Supplier<T> sx) {
128 hooks.put(name, () -> {
129
130 Log.log("shutdown.autopersist", ()->"saving "+name);
131
132 boolean success = true;
133 try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File(dir + name+"_tmp"))))) {
134 Log.log("shutdown.autopersist", ()->" writing "+sx.get());
135 oos.writeObject(sx.get());
136 } catch (Throwable e2) {
137 e2.printStackTrace();
138 success = false;
139 }
140 if (success)
141 {
142 try {
143 Log.log("shutdown.autopersist", ()->" moving into place ");
144 Files.move(Paths.get(dir+name+"_tmp"), Paths.get(dir+name), StandardCopyOption.ATOMIC_MOVE);
145 } catch (IOException e) {
146 Log.log("shutdown.autopersist", ()->e);
147 }
148 Log.log("shutdown.autopersist", ()->" finished ");
149 }
150 });
151 return x;
152 }
153
154}

Callers 2

persistMethod · 0.95
jquery.color.jsFile · 0.80

Calls 4

logMethod · 0.95
getMethod · 0.65
putMethod · 0.45
writeObjectMethod · 0.45

Tested by

no test coverage detected