Java Code Examples for com.facebook.common.internal.Preconditions#checkNotNull()

The following examples show how to use com.facebook.common.internal.Preconditions#checkNotNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CountingMemoryCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Caches given key value pair. After this method returns, caller should use returned instance
 * of CloseableReference, which depending on implementation might or might not be the same as
 * the one provided by caller. Caller is responsible for calling release method to notify
 * cache that cached value won't be used anymore. This allows the cache to maintain value's
 * in-use count.
 */
public CloseableReference<V> cache(final K key, final CloseableReference<V> value) {
  Preconditions.checkNotNull(key);
  Preconditions.checkNotNull(value);

  maybeUpdateCacheParams();
  /**
   * We might be required to remove old entry associated with the same key.
   */
  CloseableReference<V> removedValue = null;
  CacheEntry<K, V> newCacheEntry = null;

  synchronized (this) {
    if (!canCacheNewValue(value)) {
      return null;
    }

    newCacheEntry = CacheEntry.of(key, value.clone());
    removedValue = handleIndexRegistration(key, newCacheEntry.value);
    putInCachedEntries(newCacheEntry);
    increaseUsageCount(newCacheEntry);
  }

  if (removedValue != null) {
    removedValue.close();
  }

  maybeEvictEntries();

  return newCacheEntry.value;
}
 
Example 2
Source File: JfifUtil.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 *  Reads the content of the input stream until specified marker is found. Marker will be
 *  consumed and the input stream will be positioned after the specified marker.
 *  @param is the input stream to read bytes from
 *  @param markerToFind the marker we are looking for
 *  @return boolean: whether or not we found the expected marker from input stream.
 */
public static boolean moveToMarker(InputStream is, int markerToFind) throws IOException {
  Preconditions.checkNotNull(is);
  // ISO/IEC 10918-1:1993(E)
  while (StreamProcessor.readPackedInt(is, 1, false) == MARKER_FIRST_BYTE) {
    int marker = MARKER_FIRST_BYTE;
    while (marker == MARKER_FIRST_BYTE) {
      marker = StreamProcessor.readPackedInt(is, 1, false);
    }

    if (markerToFind == MARKER_SOFn && isSOFn(marker)) {
      return true;
    }
    if (marker == markerToFind) {
      return true;
    }

    // Check if the marker is SOI or TEM. These two don't have length field, so we skip it.
    if (marker == MARKER_SOI || marker == MARKER_TEM) {
      continue;
    }

    // Check if the marker is EOI or SOS. We will stop reading since metadata markers don't
    // come after these two markers.
    if (marker == MARKER_EOI || marker == MARKER_SOS) {
      return false;
    }

    // read block length
    // subtract 2 as length contain SIZE field we just read
    int length = StreamProcessor.readPackedInt(is, 2, false) - 2;
    // Skip other markers.
    is.skip(length);
  }
  return false;
}
 
Example 3
Source File: CloseableAnimatedBitmap.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
public CloseableAnimatedBitmap(
    List<CloseableReference<Bitmap>> bitmapReferences,
    List<Integer> durations) {
  Preconditions.checkNotNull(bitmapReferences);
  Preconditions.checkState(bitmapReferences.size() >= 1, "Need at least 1 frame!");
  mBitmapReferences = Lists.newArrayList();
  mBitmaps = Lists.newArrayList();
  for (CloseableReference<Bitmap> bitmapReference : bitmapReferences) {
    mBitmapReferences.add(bitmapReference.clone());
    mBitmaps.add(bitmapReference.get());
  }
  mDurations = Preconditions.checkNotNull(durations);
  Preconditions.checkState(mDurations.size() == mBitmaps.size(), "Arrays length mismatch!");
}
 
Example 4
Source File: DefaultImageFormatChecker.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Tries to match imageHeaderByte and headerSize against every known image format. If any match
 * succeeds, corresponding ImageFormat is returned.
 *
 * @param headerBytes the header bytes to check
 * @param headerSize the available header size
 * @return ImageFormat for given imageHeaderBytes or UNKNOWN if no such type could be recognized
 */
@Nullable
@Override
public final ImageFormat determineFormat(byte[] headerBytes, int headerSize) {
  Preconditions.checkNotNull(headerBytes);

  if (WebpSupportStatus.isWebpHeader(headerBytes, 0, headerSize)) {
    return getWebpFormat(headerBytes, headerSize);
  }

  if (isJpegHeader(headerBytes, headerSize)) {
    return DefaultImageFormats.JPEG;
  }

  if (isPngHeader(headerBytes, headerSize)) {
    return DefaultImageFormats.PNG;
  }

  if (isGifHeader(headerBytes, headerSize)) {
    return DefaultImageFormats.GIF;
  }

  if (isBmpHeader(headerBytes, headerSize)) {
    return DefaultImageFormats.BMP;
  }

  if (isIcoHeader(headerBytes, headerSize)) {
    return DefaultImageFormats.ICO;
  }

  if (isHeifHeader(headerBytes, headerSize)) {
    return DefaultImageFormats.HEIF;
  }

  if (isDngHeader(headerBytes, headerSize)) {
    return DefaultImageFormats.DNG;
  }

  return ImageFormat.UNKNOWN;
}
 
Example 5
Source File: CountingMemoryCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Queues given ReferenceWrapper in mEvictionQueue if its in-use count is equal to zero.
 */
private synchronized void maybeAddToEvictionQueue(final CacheEntry<K, V> cacheEntry) {
  AtomicInteger counter = mCachedEntries.get(cacheEntry);
  Preconditions.checkNotNull(counter);
  Preconditions.checkArgument(!mEvictionQueue.contains(cacheEntry));
  if (counter.get() == 0) {
    mEvictionQueueSize += mValueInfoCallback.getSizeInBytes(cacheEntry.value.get());
    mEvictionQueue.add(cacheEntry);
  }
}
 
Example 6
Source File: GenericDraweeHierarchy.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
private static Drawable maybeWrapWithScaleType(
    Drawable drawable,
    @Nullable ScaleType scaleType,
    @Nullable PointF focusPoint) {
  Preconditions.checkNotNull(drawable);
  if (scaleType == null) {
    return drawable;
  }
  ScaleTypeDrawable scaleTypeDrawable = new ScaleTypeDrawable(drawable, scaleType);
  if (focusPoint != null) {
    scaleTypeDrawable.setFocusPoint(focusPoint);
  }
  return scaleTypeDrawable;
}
 
Example 7
Source File: DraweeHolder.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the drawee hierarchy.
 */
public void setHierarchy(DH hierarchy) {
  mEventTracker.recordEvent(Event.ON_SET_HIERARCHY);
  setVisibilityCallback(null);
  mHierarchy = Preconditions.checkNotNull(hierarchy);
  onVisibilityChange(mHierarchy.getTopLevelDrawable().isVisible());
  setVisibilityCallback(this);
  if (mController != null) {
    mController.setHierarchy(hierarchy);
  }
}
 
Example 8
Source File: CountingMemoryCache.java    From fresco with MIT License 5 votes vote down vote up
/** Called when the client closes its reference. */
private void releaseClientReference(final Entry<K, V> entry) {
  Preconditions.checkNotNull(entry);
  boolean isExclusiveAdded;
  CloseableReference<V> oldRefToClose;
  synchronized (this) {
    decreaseClientCount(entry);
    isExclusiveAdded = maybeAddToExclusives(entry);
    oldRefToClose = referenceToClose(entry);
  }
  CloseableReference.closeSafely(oldRefToClose);
  maybeNotifyExclusiveEntryInsertion(isExclusiveAdded ? entry : null);
  maybeUpdateCacheParams();
  maybeEvictEntries();
}
 
Example 9
Source File: MultiDraweeHolder.java    From fresco with MIT License 5 votes vote down vote up
public void add(int index, DraweeHolder<DH> holder) {
  Preconditions.checkNotNull(holder);
  Preconditions.checkElementIndex(index, mHolders.size() + 1);
  mHolders.add(index, holder);
  if (mIsAttached) {
    holder.onAttach();
  }
}
 
Example 10
Source File: BufferMemoryChunk.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void copy(
    final int offset, final MemoryChunk other, final int otherOffset, final int count) {
  Preconditions.checkNotNull(other);
  // This implementation acquires locks on this and other objects and then delegates to
  // doCopy which does actual copy. In order to avoid deadlocks we have to establish some linear
  // order on all BufferMemoryChunks and acquire locks according to this order. In order
  // to do that, we use unique ids.
  // So we have to address 3 cases:

  // Case 1: other buffer equals this buffer, id comparison
  if (other.getUniqueId() == getUniqueId()) {
    // we do not allow copying to the same address
    // lets log warning and not copy
    Log.w(
        TAG,
        "Copying from BufferMemoryChunk "
            + Long.toHexString(getUniqueId())
            + " to BufferMemoryChunk "
            + Long.toHexString(other.getUniqueId())
            + " which are the same ");
    Preconditions.checkArgument(false);
  }

  // Case 2: Other memory chunk id < this memory chunk id
  if (other.getUniqueId() < getUniqueId()) {
    synchronized (other) {
      synchronized (this) {
        doCopy(offset, other, otherOffset, count);
      }
    }
    return;
  }

  // Case 3: Other memory chunk id > this memory chunk id
  synchronized (this) {
    synchronized (other) {
      doCopy(offset, other, otherOffset, count);
    }
  }
}
 
Example 11
Source File: NativeRoundingFilter.java    From fresco with MIT License 4 votes vote down vote up
@DoNotStrip
public static void toCircleFast(Bitmap bitmap, boolean antiAliased) {
  Preconditions.checkNotNull(bitmap);
  nativeToCircleFastFilter(bitmap, antiAliased);
}
 
Example 12
Source File: NativeMemoryChunk.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Copy bytes from native memory wrapped by this NativeMemoryChunk instance to
 * native memory wrapped by other NativeMemoryChunk
 * @param offset number of first byte to copy
 * @param other other NativeMemoryChunk to copy to
 * @param otherOffset number of first byte to write to
 * @param count number of bytes to copy
 */
public void copy(
    final int offset,
    final NativeMemoryChunk other,
    final int otherOffset,
    final int count) {
  Preconditions.checkNotNull(other);

  // This implementation acquires locks on this and other objects and then delegates to
  // doCopy which does actual copy. In order to avoid deadlocks we have to establish some linear
  // order on all NativeMemoryChunks and acquire locks according to this order. Fortunately
  // we can use mNativePtr for that purpose. So we have to address 3 cases:

  // Case 1: other memory chunk == this memory chunk
  if (other.mNativePtr == mNativePtr) {
    // we do not allow copying to the same address
    // lets log warning and not copy
    Log.w(
        TAG,
        "Copying from NativeMemoryChunk " +
            Integer.toHexString(System.identityHashCode(this)) +
            " to NativeMemoryChunk " +
            Integer.toHexString(System.identityHashCode(other)) +
            " which share the same address " +
            Long.toHexString(mNativePtr));
    Preconditions.checkArgument(false);
  }

  // Case 2: other memory chunk < this memory chunk
  if (other.mNativePtr < mNativePtr) {
    synchronized (other) {
      synchronized (this) {
        doCopy(offset, other, otherOffset, count);
      }
    }
    return;
  }

  // Case 3: other memory chunk > this memory chunk
  synchronized (this) {
    synchronized (other) {
      doCopy(offset, other, otherOffset, count);
    }
  }
}
 
Example 13
Source File: DummyTrackingInUseBitmapPool.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void release(Bitmap value) {
  Preconditions.checkNotNull(value);
  mInUseValues.remove(value);
  value.recycle();
}
 
Example 14
Source File: FileBinaryResource.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
private FileBinaryResource(File file) {
  mFile = Preconditions.checkNotNull(file);
}
 
Example 15
Source File: Instrumentation.java    From fresco with MIT License 4 votes vote down vote up
public void init(final String tag, final PerfListener perfListener) {
  mTag = Preconditions.checkNotNull(tag);
  mPerfListener = Preconditions.checkNotNull(perfListener);
}
 
Example 16
Source File: SimpleDraweeView.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
private void init() {
  Preconditions.checkNotNull(
      sDraweeControllerBuilderSupplier,
      "SimpleDraweeView was not initialized!");
  mSimpleDraweeControllerBuilder = sDraweeControllerBuilderSupplier.get();
}
 
Example 17
Source File: PoolConfig.java    From fresco with MIT License 4 votes vote down vote up
public Builder setSmallByteArrayPoolStatsTracker(
    PoolStatsTracker smallByteArrayPoolStatsTracker) {
  mSmallByteArrayPoolStatsTracker = Preconditions.checkNotNull(smallByteArrayPoolStatsTracker);
  return this;
}
 
Example 18
Source File: RoundingParams.java    From FanXin-based-HuanXin with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Sets the rounded corners radii.
 *
 * @param radii float array of 8 radii in pixels. Each corner receives two radius values [X, Y].
 *     The corners are ordered top-left, top-right, bottom-right, bottom-left.
 * @return modified instance
 */
public RoundingParams setCornersRadii(float[] radii) {
  Preconditions.checkNotNull(radii);
  Preconditions.checkArgument(radii.length == 8, "radii should have exactly 8 values");
  System.arraycopy(radii, 0, getOrCreateRoundedCornersRadii(), 0, 8);
  return this;
}
 
Example 19
Source File: MemoryPooledByteBufferOutputStream.java    From fresco with MIT License 3 votes vote down vote up
/**
 * Construct a new instance of this output stream with this initial capacity It is not an error to
 * have this initial capacity be inaccurate. If the actual contents end up being larger than the
 * initialCapacity, then we will reallocate memory if needed. If the actual contents are smaller,
 * then we'll end up wasting some memory
 *
 * @param pool the pool to use
 * @param initialCapacity initial capacity to allocate for this stream
 */
public MemoryPooledByteBufferOutputStream(MemoryChunkPool pool, int initialCapacity) {
  super();

  Preconditions.checkArgument(initialCapacity > 0);
  mPool = Preconditions.checkNotNull(pool);
  mCount = 0;
  mBufRef = CloseableReference.of(mPool.get(initialCapacity), mPool);
}
 
Example 20
Source File: ScaleTypeDrawable.java    From fresco with MIT License 2 votes vote down vote up
/**
 * Creates a new ScaleType drawable with given underlying drawable, scale type, and focus point.
 *
 * @param drawable underlying drawable to apply scale type on
 * @param scaleType scale type to be applied
 * @param focusPoint focus point of the image
 */
public ScaleTypeDrawable(Drawable drawable, ScaleType scaleType, @Nullable PointF focusPoint) {
  super(Preconditions.checkNotNull(drawable));
  mScaleType = scaleType;
  mFocusPoint = focusPoint;
}