Advertisement
bogdanudrescu

Init with AtomicReference

Mar 4th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1.     private static final AtomicReference<Map<String, File>> templates = new AtomicReference<>();
  2.  
  3.     private static Map<String, File> getTemplatesMap() {
  4.         Map<String, File> map = templates.get();
  5.         if (map == null) {
  6.             Collection<File> files = VisualDesignerPluginUtil.getFiles(
  7.                     VaadinDesignerConstants.TEMPLATE_FOLDER, "html");
  8.  
  9.             map = new LinkedHashMap<String, File>(files.size());
  10.             for (File file : files) {
  11.                 map.put(FilenameUtils.getBaseName(file.getName()), file);
  12.             }
  13.             // templates could be already filled by the separate thread, so
  14.             // previous work could be not needed, but it's rare and there is no
  15.             // any thread race condition.
  16.             if (templates.compareAndSet(null, map)) {
  17.                 return map;
  18.             } else {
  19.                 return templates.get();
  20.             }
  21.         } else {
  22.             return map;
  23.         }
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement