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

The following examples show how to use com.facebook.common.internal.Preconditions#checkState() . 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: CloseableImageCopier.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
private CloseableReference<CloseableImage> copyCloseableStaticBitmap(
    final CloseableReference<CloseableImage> closeableStaticBitmapRef) {
  Bitmap sourceBitmap = ((CloseableStaticBitmap) closeableStaticBitmapRef.get())
      .getUnderlyingBitmap();
  CloseableReference<Bitmap> bitmapRef = mPlatformBitmapFactory.createBitmap(
      sourceBitmap.getWidth(),
      sourceBitmap.getHeight());
  try {
    Bitmap destinationBitmap = bitmapRef.get();
    Preconditions.checkState(!destinationBitmap.isRecycled());
    Preconditions.checkState(destinationBitmap.isMutable());
    Bitmaps.copyBitmap(destinationBitmap, sourceBitmap);
    return CloseableReference.<CloseableImage>of(
        new CloseableStaticBitmap(bitmapRef, ImmutableQualityInfo.FULL_QUALITY));
  } finally {
    bitmapRef.close();
  }
}
 
Example 2
Source File: SingleByteArrayPool.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates an instance of the SingleByteArrayPool class, and registers it
 * @param memoryTrimmableRegistry the memory resource manager
 * @param singleByteArrayPoolStatsTracker stats tracker for the pool
 * @param minByteArraySize size of the smallest byte array we will create
 * @param maxByteArraySize size of the largest byte array we will create
 */
@VisibleForTesting
SingleByteArrayPool(
    MemoryTrimmableRegistry memoryTrimmableRegistry,
    SingleByteArrayPoolStatsTracker singleByteArrayPoolStatsTracker,
    int minByteArraySize,
    int maxByteArraySize) {
  Preconditions.checkNotNull(memoryTrimmableRegistry);
  Preconditions.checkNotNull(singleByteArrayPoolStatsTracker);
  Preconditions.checkState(minByteArraySize > 0 && maxByteArraySize >= minByteArraySize);

  mSingleByteArrayPoolStatsTracker = singleByteArrayPoolStatsTracker;
  mMaxByteArraySize = maxByteArraySize;
  mMinByteArraySize = minByteArraySize;
  mByteArraySoftRef = new OOMSoftReference<byte[]>();

  memoryTrimmableRegistry.registerMemoryTrimmable(this);
}
 
Example 3
Source File: NativeMemoryChunk.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copy bytes from byte array to native memory.
 * @param nativeMemoryOffset number of first byte to be written by copy operation
 * @param byteArray byte array to copy from
 * @param byteArrayOffset number of first byte in byteArray to copy
 * @param count number of bytes to copy
 * @return number of bytes written
 */
public synchronized int write(
    int nativeMemoryOffset,
    final byte[] byteArray,
    int byteArrayOffset,
    int count) {
  Preconditions.checkNotNull(byteArray);
  Preconditions.checkState(!isClosed());
  final int actualCount = adjustByteCount(nativeMemoryOffset, count);
  checkBounds(nativeMemoryOffset, byteArray.length, byteArrayOffset, actualCount);
  nativeCopyFromByteArray(
      mNativePtr + nativeMemoryOffset,
      byteArray,
      byteArrayOffset,
      actualCount);
  return actualCount;
}
 
Example 4
Source File: CloseableAnimatedBitmap.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance of a CloseableStaticBitmap.
 * @param bitmaps the bitmap frames. This list must be immutable.
 * @param durations the frame durations, This list must be immutable.
 * @param resourceReleaser ResourceReleaser to release the bitmaps to
 */
public CloseableAnimatedBitmap(
    List<Bitmap> bitmaps,
    List<Integer> durations,
    ResourceReleaser<Bitmap> resourceReleaser) {
  Preconditions.checkNotNull(bitmaps);
  Preconditions.checkState(bitmaps.size() >= 1, "Need at least 1 frame!");
  mBitmaps = Lists.newArrayList();
  mBitmapReferences = Lists.newArrayList();
  for (Bitmap bitmap : bitmaps) {
    mBitmapReferences.add(CloseableReference.of(bitmap, resourceReleaser));
    mBitmaps.add(bitmap);
  }
  mDurations = Preconditions.checkNotNull(durations);
  Preconditions.checkState(mDurations.size() == mBitmaps.size(), "Arrays length mismatch!");
}
 
Example 5
Source File: WatchImpl.java    From ImageLoadPK with Apache License 2.0 5 votes vote down vote up
public void onFailure() {
    Preconditions.checkState(mState == ImageRequestState.STARTED);
    mState = ImageRequestState.FAILURE;
    mFinishTime = System.currentTimeMillis();
    final long requestTime = mFinishTime - mStartTime;
    mWatchListener.reportFailure(requestTime);
}
 
Example 6
Source File: ListDataSource.java    From fresco with MIT License 5 votes vote down vote up
public static <T> ListDataSource<T> create(DataSource<CloseableReference<T>>... dataSources) {
  Preconditions.checkNotNull(dataSources);
  Preconditions.checkState(dataSources.length > 0);
  ListDataSource<T> listDataSource = new ListDataSource<T>(dataSources);
  for (DataSource<CloseableReference<T>> dataSource : dataSources) {
    if (dataSource != null) {
      dataSource.subscribe(
          listDataSource.new InternalDataSubscriber(), CallerThreadExecutor.getInstance());
    }
  }
  return listDataSource;
}
 
Example 7
Source File: AbstractDraweeControllerBuilder.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/** Validates the parameters before building a controller. */
protected void validate() {
  Preconditions.checkState(
      (mMultiImageRequests == null) || (mImageRequest == null),
      "Cannot specify both ImageRequest and FirstAvailableImageRequests!");
  Preconditions.checkState(
      (mDataSourceSupplier == null) ||
          (mMultiImageRequests == null && mImageRequest == null && mLowResImageRequest == null),
      "Cannot specify DataSourceSupplier with other ImageRequests! Use one or the other.");
}
 
Example 8
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 9
Source File: PooledByteArrayBufferedInputStream.java    From fresco with MIT License 5 votes vote down vote up
@Override
public int read(byte[] buffer, int offset, int length) throws IOException {
  Preconditions.checkState(mBufferOffset <= mBufferedSize);
  ensureNotClosed();
  if (!ensureDataInBuffer()) {
    return -1;
  }

  final int bytesToRead = Math.min(mBufferedSize - mBufferOffset, length);
  System.arraycopy(mByteArray, mBufferOffset, buffer, offset, bytesToRead);
  mBufferOffset += bytesToRead;
  return bytesToRead;
}
 
Example 10
Source File: NativeMemoryChunkInputStream.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new inputstream instance over the specific memory chunk.
 * @param nativeMemoryChunk the native memory chunk
 * @param startOffset start offset within the memory chunk
 * @param length length of subchunk
 */
public NativeMemoryChunkInputStream(
    NativeMemoryChunk nativeMemoryChunk,
    int startOffset,
    int length) {
  super();
  Preconditions.checkState(startOffset >= 0);
  Preconditions.checkState(length >= 0);
  mMemoryChunk = Preconditions.checkNotNull(nativeMemoryChunk);
  mStartOffset = startOffset;
  mEndOffset = startOffset + length > nativeMemoryChunk.getSize()
      ? nativeMemoryChunk.getSize() : startOffset + length;
  mOffset = startOffset;
  mMark = startOffset;
}
 
Example 11
Source File: AnimatedImageFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decodes a GIF into a CloseableImage.
 *
 * @param pooledByteBufferRef native byte array holding the encoded bytes
 * @param options the options for the decode
 * @return a {@link CloseableImage} for the GIF image
 */
public CloseableImage decodeGif(
    final CloseableReference<PooledByteBuffer> pooledByteBufferRef,
    final ImageDecodeOptions options) {
  Preconditions.checkState(!options.forceOldAnimationCode);
  final PooledByteBuffer input = pooledByteBufferRef.get();
  GifImage gifImage = GifImage.create(input.getNativePtr(), input.size());

  return getCloseableImage(options, gifImage);
}
 
Example 12
Source File: CountingMemoryCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decrements in-use counter fot given ReferenceWrapper. Only pairs with counter 0 might be
 * evicted from the cache.
 */
private synchronized void decreaseUsageCount(final CacheEntry<K, V> cacheEntry) {
  AtomicInteger counter = mCachedEntries.get(cacheEntry);
  Preconditions.checkNotNull(counter);
  Preconditions.checkState(counter.get() > 0);
  counter.decrementAndGet();
}
 
Example 13
Source File: PooledByteArrayBufferedInputStream.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long skip(long byteCount) throws IOException {
  Preconditions.checkState(mBufferOffset <= mBufferedSize);
  ensureNotClosed();
  final int bytesLeftInBuffer = mBufferedSize - mBufferOffset;
  if (bytesLeftInBuffer >= byteCount) {
    mBufferOffset += byteCount;
    return byteCount;
  }

  mBufferOffset = mBufferedSize;
  return bytesLeftInBuffer + mInputStream.skip(byteCount - bytesLeftInBuffer);
}
 
Example 14
Source File: CountingMemoryCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
private synchronized void putInCachedEntries(final CacheEntry<K, V> cacheEntry) {
  Preconditions.checkState(!mCachedEntries.containsKey(cacheEntry));
  mCachedValuesSize += mValueInfoCallback.getSizeInBytes(cacheEntry.value.get());
  mCachedEntries.put(cacheEntry, new AtomicInteger());
}
 
Example 15
Source File: CloseableReference.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a new CloseableReference to the same underlying SharedReference. The SharedReference
 * ref-count is incremented.
 */
@Override
public synchronized CloseableReference<T> clone() {
  Preconditions.checkState(isValid());
  return new CloseableReference<T>(mSharedReference);
}
 
Example 16
Source File: Bucket.java    From fresco with MIT License 4 votes vote down vote up
/**
 * Decrement the mInUseCount field. Used by the pool to update the bucket info when a value was
 * freed, instead of being returned to the bucket's free list
 */
public void decrementInUseCount() {
  Preconditions.checkState(mInUseLength > 0);
  mInUseLength--;
}
 
Example 17
Source File: CountingMemoryCache.java    From fresco with MIT License 4 votes vote down vote up
/** Increases the entry's client count. */
private synchronized void increaseClientCount(Entry<K, V> entry) {
  Preconditions.checkNotNull(entry);
  Preconditions.checkState(!entry.isOrphan);
  entry.clientCount++;
}
 
Example 18
Source File: CountingMemoryCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
private synchronized void removeFromCachedEntries(final CacheEntry<K, V> cacheEntry) {
  final long valueSize = mValueInfoCallback.getSizeInBytes(cacheEntry.value.get());
  Preconditions.checkState(mCachedValuesSize >= valueSize);
  Preconditions.checkNotNull(mCachedEntries.remove(cacheEntry));
  mCachedValuesSize -= valueSize;
}
 
Example 19
Source File: AshmemMemoryChunk.java    From fresco with MIT License 4 votes vote down vote up
@Override
public int getSize() {
  Preconditions.checkState(!isClosed());
  return mSharedMemory.getSize();
}
 
Example 20
Source File: FadeDrawable.java    From fresco with MIT License 4 votes vote down vote up
@Override
public void draw(Canvas canvas) {
  boolean done = true;
  float ratio;

  switch (mTransitionState) {
    case TRANSITION_STARTING:
      // initialize start alphas and start time
      System.arraycopy(mAlphas, 0, mStartAlphas, 0, mLayers.length);
      mStartTimeMs = getCurrentTimeMs();
      // if the duration is 0, update alphas to the target opacities immediately
      ratio = (mDurationMs == 0) ? 1.0f : 0.0f;
      // if all the layers have reached their target opacity, transition is done
      done = updateAlphas(ratio);
      mTransitionState = done ? TRANSITION_NONE : TRANSITION_RUNNING;

      if (done) {
        maybeNotifyOnFadeFinished();
      }
      break;

    case TRANSITION_RUNNING:
      Preconditions.checkState(mDurationMs > 0);
      // determine ratio based on the elapsed time
      ratio = (float) (getCurrentTimeMs() - mStartTimeMs) / mDurationMs;
      // if all the layers have reached their target opacity, transition is done
      done = updateAlphas(ratio);
      mTransitionState = done ? TRANSITION_NONE : TRANSITION_RUNNING;

      if (done) {
        maybeNotifyOnFadeFinished();
      }
      break;

    case TRANSITION_NONE:
      // there is no transition in progress and mAlphas should be left as is.
      done = true;

      maybeNotifyOnFadeFinished();
      break;
  }

  for (int i = 0; i < mLayers.length; i++) {
    drawDrawableWithAlpha(canvas, mLayers[i], mAlphas[i] * mAlpha / 255);
  }

  if (!done) {
    invalidateSelf();
  }
}