Exemplo n.º 1
0
 @Override
 public synchronized Object get() {
   Object ret = super.get();
   if (System.currentTimeMillis() > expires) {
     expired++;
     super.clear();
     ret = null;
   }
   return ret;
 }
 /**
  * Borrow an object from the pool. If there are no idle instances available in the pool, the
  * configured factory's {@link PoolableObjectFactory#makeObject()} method is invoked to create a
  * new instance.
  *
  * <p>All instances are {@link PoolableObjectFactory#activateObject(Object) activated} and {@link
  * PoolableObjectFactory#validateObject(Object) validated} before being returned by this method.
  * If validation fails or an exception occurs activating or validating an idle instance, the
  * failing instance is {@link PoolableObjectFactory#destroyObject(Object) destroyed} and another
  * instance is retrieved from the pool, validated and activated. This process continues until
  * either the pool is empty or an instance passes validation. If the pool is empty on activation
  * or it does not contain any valid instances, the factory's <code>makeObject</code> method is
  * used to create a new instance. If the created instance either raises an exception on activation
  * or fails validation, <code>NoSuchElementException</code> is thrown. Exceptions thrown by <code>
  * MakeObject</code> are propagated to the caller; but other than <code>ThreadDeath</code> or
  * <code>VirtualMachineError</code>, exceptions generated by activation, validation or destroy
  * methods are swallowed silently.
  *
  * @throws NoSuchElementException if a valid object cannot be provided
  * @throws IllegalStateException if invoked on a {@link #close() closed} pool
  * @throws Exception if an exception occurs creating a new instance
  * @return a valid, activated object instance
  */
 @Override
 public synchronized T borrowObject() throws Exception {
   assertOpen();
   T obj = null;
   boolean newlyCreated = false;
   while (null == obj) {
     if (_pool.isEmpty()) {
       if (null == _factory) {
         throw new NoSuchElementException();
       } else {
         newlyCreated = true;
         obj = _factory.makeObject();
       }
     } else {
       SoftReference<T> ref = _pool.remove(_pool.size() - 1);
       obj = ref.get();
       ref.clear(); // prevent this ref from being enqueued with refQueue.
     }
     if (null != _factory && null != obj) {
       try {
         _factory.activateObject(obj);
         if (!_factory.validateObject(obj)) {
           throw new Exception("ValidateObject failed");
         }
       } catch (Throwable t) {
         PoolUtils.checkRethrow(t);
         try {
           _factory.destroyObject(obj);
         } catch (Throwable t2) {
           PoolUtils.checkRethrow(t2);
           // Swallowed
         } finally {
           obj = null;
         }
         if (newlyCreated) {
           throw new NoSuchElementException(
               "Could not create a validated object, cause: " + t.getMessage());
         }
       }
     }
   }
   _numActive++;
   return obj;
 }
Exemplo n.º 3
0
  private static SoftReference<CachedImage> makeImage(CachedTeXFormula cached)
      throws ParseException {
    TeXFormula formula = new TeXFormula(cached.f);
    TeXIcon icon = formula.createTeXIcon(cached.style, cached.size, cached.type, cached.fgcolor);
    icon.setInsets(new Insets(cached.inset, cached.inset, cached.inset, cached.inset));
    Image image =
        new Graphics().createImage(icon.getIconWidth(), icon.getIconHeight(), Image.TYPE_INT_ARGB);
    Graphics2DInterface g2 = image.createGraphics2D();
    icon.paintIcon(null, g2, 0, 0);
    g2.dispose();
    cached.setDimensions(icon.getIconWidth(), icon.getIconHeight(), icon.getIconDepth());
    SoftReference<CachedImage> img =
        new SoftReference<CachedImage>(new CachedImage(image, cached), queue);

    if (cache.size() >= max) {
      Reference soft;
      while ((soft = queue.poll()) != null) {
        CachedImage ci = (CachedImage) soft.get();
        if (ci != null) {
          cache.remove(ci.cachedTf);
        }
      }
      Iterator<CachedTeXFormula> iter = cache.keySet().iterator();
      if (iter.hasNext()) {
        CachedTeXFormula c = iter.next();
        SoftReference<CachedImage> cachedImage = cache.get(c);
        if (cachedImage != null) {
          cachedImage.clear();
        }
        cache.remove(c);
      }
    }
    cache.put(cached, img);

    return img;
  }
Exemplo n.º 4
0
 @Override
 protected void fireUpdate() {
   if (imageCache != null) imageCache.clear();
   super.fireUpdate();
 }
Exemplo n.º 5
0
 /** Flushes the cached object. Forces the next invocation of get() to invoke reconstitute(). */
 public synchronized void flush() {
   SoftReference s = soft;
   if (s != null) s.clear();
   soft = null;
 }
Exemplo n.º 6
0
  /**
   * Notifies that the preview's configuration has changed.
   *
   * @param flags the change flags, a bitmask corresponding to the {@code CHANGE_} constants in
   *     {@link ConfigurationClient}
   */
  public void configurationChanged(int flags) {
    if (!mVisible) {
      mDirty |= flags;
      return;
    }

    if ((flags & MASK_RENDERING) != 0) {
      mResourceResolver.clear();
      // Handle inheritance
      mConfiguration.syncFolderConfig();
      updateForkStatus();
      updateSize();
    }

    // Sanity check to make sure things are working correctly
    if (DEBUG) {
      RenderPreviewMode mode = mManager.getMode();
      if (mode == DEFAULT) {
        assert mConfiguration instanceof VaryingConfiguration;
        VaryingConfiguration config = (VaryingConfiguration) mConfiguration;
        int alternateFlags = config.getAlternateFlags();
        switch (alternateFlags) {
          case Configuration.CFG_DEVICE_STATE:
            {
              State configState = config.getDeviceState();
              State chooserState = mManager.getChooser().getConfiguration().getDeviceState();
              assert configState != null && chooserState != null;
              assert !configState.getName().equals(chooserState.getName())
                  : configState.toString() + ':' + chooserState;

              Device configDevice = config.getDevice();
              Device chooserDevice = mManager.getChooser().getConfiguration().getDevice();
              assert configDevice != null && chooserDevice != null;
              assert configDevice == chooserDevice : configDevice.toString() + ':' + chooserDevice;

              break;
            }
          case Configuration.CFG_DEVICE:
            {
              Device configDevice = config.getDevice();
              Device chooserDevice = mManager.getChooser().getConfiguration().getDevice();
              assert configDevice != null && chooserDevice != null;
              assert configDevice != chooserDevice : configDevice.toString() + ':' + chooserDevice;

              State configState = config.getDeviceState();
              State chooserState = mManager.getChooser().getConfiguration().getDeviceState();
              assert configState != null && chooserState != null;
              assert configState.getName().equals(chooserState.getName())
                  : configState.toString() + ':' + chooserState;

              break;
            }
          case Configuration.CFG_LOCALE:
            {
              Locale configLocale = config.getLocale();
              Locale chooserLocale = mManager.getChooser().getConfiguration().getLocale();
              assert configLocale != null && chooserLocale != null;
              assert configLocale != chooserLocale : configLocale.toString() + ':' + chooserLocale;
              break;
            }
          default:
            {
              // Some other type of override I didn't anticipate
              assert false : alternateFlags;
            }
        }
      }
    }

    mDirty = 0;
    mManager.scheduleRender(this);
  }