You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsyncBundleLoader.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.metadata;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import com.vaadin.client.Profiler;
  21. public abstract class AsyncBundleLoader {
  22. public enum State {
  23. NOT_STARTED, LOADING, LOADED, ERROR;
  24. }
  25. private State state = State.NOT_STARTED;
  26. private Throwable error = null;
  27. private List<BundleLoadCallback> callbacks = new ArrayList<>();
  28. private final String packageName;
  29. private final String[] indentifiers;
  30. public AsyncBundleLoader(String packageName, String[] indentifiers) {
  31. this.packageName = packageName;
  32. this.indentifiers = indentifiers;
  33. }
  34. protected abstract void load(TypeDataStore store);
  35. public List<BundleLoadCallback> setError(Throwable error) {
  36. assert state == State.LOADING;
  37. state = State.ERROR;
  38. this.error = error;
  39. return clearCallbacks();
  40. }
  41. public Throwable getError() {
  42. return error;
  43. }
  44. public State getState() {
  45. return state;
  46. }
  47. public List<BundleLoadCallback> getCallback() {
  48. return Collections.unmodifiableList(callbacks);
  49. }
  50. public void load(BundleLoadCallback callback, TypeDataStore store) {
  51. assert state == State.NOT_STARTED;
  52. Profiler.enter("AsyncBundleLoader.load");
  53. state = State.LOADING;
  54. addCallback(callback);
  55. load(store);
  56. Profiler.leave("AsyncBundleLoader.load");
  57. }
  58. public void addCallback(BundleLoadCallback callback) {
  59. assert state == State.LOADING;
  60. if (callback != null) {
  61. callbacks.add(callback);
  62. }
  63. }
  64. public List<BundleLoadCallback> setLoaded() {
  65. assert state == State.LOADING;
  66. state = State.LOADED;
  67. return clearCallbacks();
  68. }
  69. private List<BundleLoadCallback> clearCallbacks() {
  70. List<BundleLoadCallback> callbacks = this.callbacks;
  71. this.callbacks = null;
  72. return callbacks;
  73. }
  74. public String getName() {
  75. return packageName;
  76. }
  77. public String[] getIndentifiers() {
  78. return indentifiers;
  79. }
  80. }