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

The following examples show how to use com.facebook.common.internal.Preconditions#checkArgument() . 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: ImageFormatChecker.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks if byteArray interpreted as sequence of bytes has a subsequence equal to pattern
 * starting at position equal to offset.
 * @param byteArray
 * @param offset
 * @param pattern
 * @return true if match succeeds, false otherwise
 */
private static boolean matchBytePattern(
    final byte[] byteArray,
    final int offset,
    final byte[] pattern) {
  Preconditions.checkNotNull(byteArray);
  Preconditions.checkNotNull(pattern);
  Preconditions.checkArgument(offset >= 0);
  if (pattern.length + offset > byteArray.length) {
    return false;
  }

  for (int i = 0; i < pattern.length; ++i) {
    if (byteArray[i + offset] != pattern[i]) {
      return false;
    }
  }

  return true;
}
 
Example 2
Source File: SharedByteArray.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Get exclusive access to the byte array of size greater or equal to the passed one.
 *
 * <p>Under the hood this method acquires an exclusive lock that is released when the returned
 * reference is closed.
 */
public CloseableReference<byte[]> get(int size) {
  Preconditions.checkArgument(size > 0, "Size must be greater than zero");
  Preconditions.checkArgument(size <= mMaxByteArraySize, "Requested size is too big");
  mSemaphore.acquireUninterruptibly();
  try {
    byte[] byteArray = getByteArray(size);
    return CloseableReference.of(byteArray, mResourceReleaser);
  } catch (Throwable t) {
    mSemaphore.release();
    throw Throwables.propagate(t);
  }
}
 
Example 3
Source File: RoundedCornersDrawable.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets radii values to be used for rounding.
 * Each corner receive two radius values [X, Y]. The corners are ordered
 * top-left, top-right, bottom-right, bottom-left
 *
 * @param radii Array of 8 values, 4 pairs of [X,Y] radii
 */
public void setRadii(float[] radii) {
  if (radii == null) {
    Arrays.fill(mRadii, 0);
  } else {
    Preconditions.checkArgument(radii.length == 8, "radii should have exactly 8 values");
    System.arraycopy(radii, 0, mRadii, 0, 8);
  }
  updatePath();
  invalidateSelf();
}
 
Example 4
Source File: BufferMemoryChunk.java    From fresco with MIT License 5 votes vote down vote up
@Override
public synchronized byte read(final int offset) {
  Preconditions.checkState(!isClosed());
  Preconditions.checkArgument(offset >= 0);
  Preconditions.checkArgument(offset < mSize);
  return mBuffer.get(offset);
}
 
Example 5
Source File: AbstractDraweeController.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Sets the hierarchy.
 *
 * <p>The controller should be detached when this method is called.
 *
 * @param hierarchy This must be an instance of {@link SettableDraweeHierarchy}
 */
@Override
public void setHierarchy(@Nullable DraweeHierarchy hierarchy) {
  if (FLog.isLoggable(FLog.VERBOSE)) {
    FLog.v(
        TAG, "controller %x %s: setHierarchy: %s", System.identityHashCode(this), mId, hierarchy);
  }
  mEventTracker.recordEvent(
      (hierarchy != null) ? Event.ON_SET_HIERARCHY : Event.ON_CLEAR_HIERARCHY);
  // force release in case request was submitted
  if (mIsRequestSubmitted) {
    mDeferredReleaser.cancelDeferredRelease(this);
    release();
  }
  // clear the existing hierarchy
  if (mSettableDraweeHierarchy != null) {
    mSettableDraweeHierarchy.setControllerOverlay(null);
    mSettableDraweeHierarchy = null;
  }
  // set the new hierarchy
  if (hierarchy != null) {
    Preconditions.checkArgument(hierarchy instanceof SettableDraweeHierarchy);
    mSettableDraweeHierarchy = (SettableDraweeHierarchy) hierarchy;
    mSettableDraweeHierarchy.setControllerOverlay(mControllerOverlay);
  }

  if (mLoggingListener != null) {
    setUpLoggingListener();
  }
}
 
Example 6
Source File: Bitmaps.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This blits the pixel data from src to dest.
 * <p>The destination bitmap must have both a height and a width equal to the source. For maximum
 * speed stride should be equal as well.
 * <p>Both bitmaps must be in {@link android.graphics.Bitmap.Config#ARGB_8888} format.
 * <p>If the src is purgeable, it will be decoded as part of this operation if it was purged.
 * The dest should not be purgeable. If it is, the copy will still take place,
 * but will be lost the next time the dest gets purged, without warning.
 * <p>The dest must be mutable.
 * @param dest Bitmap to copy into
 * @param src Bitmap to copy out of
 */
public static void copyBitmap(Bitmap dest, Bitmap src) {
  Preconditions.checkArgument(src.getConfig() == Bitmap.Config.ARGB_8888);
  Preconditions.checkArgument(dest.getConfig() == Bitmap.Config.ARGB_8888);
  Preconditions.checkArgument(dest.isMutable());
  Preconditions.checkArgument(dest.getWidth() == src.getWidth());
  Preconditions.checkArgument(dest.getHeight() == src.getHeight());
  nativeCopyBitmap(
      dest,
      dest.getRowBytes(),
      src,
      src.getRowBytes(),
      dest.getHeight());
}
 
Example 7
Source File: MemoryPooledByteBufferFactory.java    From fresco with MIT License 5 votes vote down vote up
@Override
public MemoryPooledByteBuffer newByteBuffer(int size) {
  Preconditions.checkArgument(size > 0);
  CloseableReference<MemoryChunk> chunkRef = CloseableReference.of(mPool.get(size), mPool);
  try {
    return new MemoryPooledByteBuffer(chunkRef, size);
  } finally {
    chunkRef.close();
  }
}
 
Example 8
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 9
Source File: BitmapCounter.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Excludes given bitmap from the count.
 *
 * @param bitmap to be excluded from the count
 */
public synchronized void decrease(Bitmap bitmap) {
  final int bitmapSize = BitmapUtil.getSizeInBytes(bitmap);
  Preconditions.checkArgument(mCount > 0, "No bitmaps registered.");
  Preconditions.checkArgument(
      bitmapSize <= mSize,
      "Bitmap size bigger than the total registered size: %d, %d",
      bitmapSize,
      mSize);
  mSize -= bitmapSize;
  mCount--;
}
 
Example 10
Source File: RoundedColorDrawable.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Sets the rounding radii.
 *
 * @param radii Each corner receive two radius values [X, Y]. The corners are ordered top-left,
 *     top-right, bottom-right, bottom-left
 */
@Override
public void setRadii(float[] radii) {
  if (radii == null) {
    Arrays.fill(mRadii, 0);
  } else {
    Preconditions.checkArgument(radii.length == 8, "radii should have exactly 8 values");
    System.arraycopy(radii, 0, mRadii, 0, 8);
  }
  updatePath();
  invalidateSelf();
}
 
Example 11
Source File: StagingArea.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores key-value in this StagingArea. This call overrides previous value
 * of stored reference if
 * @param key
 * @param bufferRef reference to be associated with key
 */
public synchronized void put(
    final CacheKey key,
    final CloseableReference<PooledByteBuffer> bufferRef) {
  Preconditions.checkNotNull(key);
  Preconditions.checkArgument(CloseableReference.isValid(bufferRef));

  // we're making a 'copy' of this reference - so duplicate it
  final CloseableReference<?> oldEntry = mMap.put(key, bufferRef.clone());
  if (oldEntry != null) {
    oldEntry.close();
  }
  logStats();
}
 
Example 12
Source File: BitmapPrepareProducer.java    From fresco with MIT License 5 votes vote down vote up
/**
 * @param inputProducer The next producer in the pipeline
 * @param minBitmapSizeBytes Bitmaps with a {@link Bitmap#getByteCount()} smaller than this value
 *     are not uploaded
 * @param maxBitmapSizeBytes Bitmaps with a {@link Bitmap#getByteCount()} larger than this value
 *     are not uploaded
 */
public BitmapPrepareProducer(
    final Producer<CloseableReference<CloseableImage>> inputProducer,
    int minBitmapSizeBytes,
    int maxBitmapSizeBytes,
    boolean preparePrefetch) {
  Preconditions.checkArgument(minBitmapSizeBytes <= maxBitmapSizeBytes);
  mInputProducer = Preconditions.checkNotNull(inputProducer);
  mMinBitmapSizeBytes = minBitmapSizeBytes;
  mMaxBitmapSizeBytes = maxBitmapSizeBytes;
  mPreparePrefetch = preparePrefetch;
}
 
Example 13
Source File: DownsampleUtil.java    From fresco with MIT License 5 votes vote down vote up
private static int getRotationAngle(
    final RotationOptions rotationOptions, final EncodedImage encodedImage) {
  if (!rotationOptions.useImageMetadata()) {
    return 0;
  }
  int rotationAngle = encodedImage.getRotationAngle();
  Preconditions.checkArgument(
      rotationAngle == 0 || rotationAngle == 90 || rotationAngle == 180 || rotationAngle == 270);
  return rotationAngle;
}
 
Example 14
Source File: ArrayDrawable.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Gets the drawable at the specified index.
 *
 * @param index index of drawable to get
 * @return drawable at the specified index
 */
@Nullable
public Drawable getDrawable(int index) {
  Preconditions.checkArgument(index >= 0);
  Preconditions.checkArgument(index < mLayers.length);
  return mLayers[index];
}
 
Example 15
Source File: RoundedColorDrawable.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the rounding radius.
 *
 * @param radius
 */
public void setRadius(float radius) {
  Preconditions.checkArgument(radius >= 0, "radius should be non negative");
  Arrays.fill(mRadii, radius);
  updatePath();
  invalidateSelf();
}
 
Example 16
Source File: NativeMemoryChunk.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
public NativeMemoryChunk(final int size) {
  Preconditions.checkArgument(size > 0);
  mSize = size;
  mNativePtr = nativeAllocate(mSize);
  mClosed = false;
}
 
Example 17
Source File: FirstAvailableDataSourceSupplier.java    From fresco with MIT License 4 votes vote down vote up
private FirstAvailableDataSourceSupplier(List<Supplier<DataSource<T>>> dataSourceSuppliers) {
  Preconditions.checkArgument(!dataSourceSuppliers.isEmpty(), "List of suppliers is empty!");
  mDataSourceSuppliers = dataSourceSuppliers;
}
 
Example 18
Source File: NativePooledByteBuffer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
public NativePooledByteBuffer(CloseableReference<NativeMemoryChunk> bufRef, int size) {
  Preconditions.checkNotNull(bufRef);
  Preconditions.checkArgument(size >= 0 && size <= bufRef.get().getSize());
  mBufRef = bufRef.clone();
  mSize = size;
}
 
Example 19
Source File: RoundingParams.java    From fresco with MIT License 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 20
Source File: PlatformBitmapFactory.java    From fresco with MIT License 2 votes vote down vote up
/**
 * Common code for checking that width and height are > 0
 *
 * @param width width to ensure is > 0
 * @param height height to ensure is > 0
 */
private static void checkWidthHeight(int width, int height) {
  Preconditions.checkArgument(width > 0, "width must be > 0");
  Preconditions.checkArgument(height > 0, "height must be > 0");
}