Convert Figma logo to code with AI

twitter logoutil

Wonderful reusable code from Twitter

2,685
580
2,685
12

Top Related Projects

13,024

Build highly concurrent, distributed, and resilient message-driven applications on the JVM

33,257

Netty project - an event-driven asynchronous network application framework

47,834

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

Spring Framework

49,988

Google core libraries for Java

Apache Commons Lang

Quick Overview

Twitter's Util is a collection of open-source utility libraries for Scala, designed to complement and extend the Scala standard library. It provides a wide range of tools and abstractions for concurrent programming, time manipulation, statistics, and more, originally developed and used internally at Twitter.

Pros

  • Comprehensive set of utilities covering various aspects of Scala development
  • Battle-tested in production environments at Twitter
  • Well-documented and actively maintained
  • Seamless integration with other Twitter open-source projects

Cons

  • Learning curve can be steep for developers new to Scala or functional programming
  • Some utilities may be Twitter-specific and less applicable in other contexts
  • Dependency management can be complex due to the modular nature of the library
  • May introduce additional complexity to smaller projects that don't require such extensive utilities

Code Examples

  1. Using Future for asynchronous operations:
import com.twitter.util.{Future, FuturePool}

val result: Future[Int] = FuturePool.unboundedPool {
  // Simulate a long-running operation
  Thread.sleep(1000)
  42
}

result.onSuccess { value =>
  println(s"The result is: $value")
}
  1. Working with Time and Duration:
import com.twitter.util.{Time, Duration}

val now = Time.now
val tenSecondsLater = now + Duration.fromSeconds(10)

println(s"Current time: $now")
println(s"10 seconds later: $tenSecondsLater")
  1. Using Stats for metrics:
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.finagle.stats.NullStatsReceiver

val stats: StatsReceiver = NullStatsReceiver

val counter = stats.counter("requests")
counter.incr()

val gauge = stats.addGauge("queue_size") { 
  // Return the current queue size
  10
}

Getting Started

To use Twitter's Util in your Scala project, add the following dependencies to your build.sbt file:

libraryDependencies ++= Seq(
  "com.twitter" %% "util-core" % "21.4.0",
  "com.twitter" %% "util-collection" % "21.4.0",
  "com.twitter" %% "util-stats" % "21.4.0"
)

Then, import the required utilities in your Scala code:

import com.twitter.util._

You can now start using the various utilities provided by the library in your project.

Competitor Comparisons

13,024

Build highly concurrent, distributed, and resilient message-driven applications on the JVM

Pros of Akka

  • More comprehensive actor-based concurrency model
  • Better suited for distributed systems and cluster computing
  • Stronger focus on fault tolerance and resilience

Cons of Akka

  • Steeper learning curve due to its complexity
  • Heavier footprint and potential performance overhead
  • Less integration with Twitter-specific infrastructure

Code Comparison

Akka example (Actor creation):

class MyActor extends Actor {
  def receive = {
    case "hello" => println("Hello, world!")
    case _       => println("Unknown message")
  }
}
val system = ActorSystem("MySystem")
val myActor = system.actorOf(Props[MyActor], "myactor")

Util example (Future composition):

import com.twitter.util.Future

val future1 = Future { "Hello" }
val future2 = Future { "World" }
val combinedFuture = future1.join(future2).map { case (a, b) => s"$a, $b!" }

Summary

Akka is a more comprehensive solution for building distributed, concurrent, and resilient systems, while Util provides a lightweight set of utilities primarily focused on Twitter's infrastructure. Akka offers a robust actor model and cluster capabilities, but comes with increased complexity. Util is simpler and more tightly integrated with Twitter's ecosystem but may not be as suitable for large-scale distributed applications.

33,257

Netty project - an event-driven asynchronous network application framework

Pros of Netty

  • More comprehensive networking framework with support for various protocols
  • Higher performance and scalability for large-scale applications
  • Extensive documentation and larger community support

Cons of Netty

  • Steeper learning curve due to its complexity
  • Potentially overkill for simpler networking tasks
  • Requires more boilerplate code for basic setups

Code Comparison

Netty server setup:

EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
 .channel(NioServerSocketChannel.class)

Util server setup (using Finagle, which is built on Util):

val server = Http.server
  .withLabel("myServer")
  .serve(":8080", myService)

Summary

Netty is a more powerful and flexible networking framework, suitable for complex, high-performance applications. It offers broader protocol support and better scalability but comes with a steeper learning curve.

Util, on the other hand, provides a simpler, more concise API for basic networking tasks, making it easier to use for smaller projects or quick prototypes. However, it may lack some advanced features and performance optimizations found in Netty.

The choice between the two depends on the specific requirements of your project, such as performance needs, scalability, and the complexity of the networking tasks involved.

47,834

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

Pros of RxJava

  • More comprehensive and feature-rich reactive programming library
  • Extensive documentation and community support
  • Cross-platform compatibility (Android, Java, Kotlin)

Cons of RxJava

  • Steeper learning curve due to its extensive API
  • Can be overkill for simpler projects
  • Potential performance overhead for complex operations

Code Comparison

util:

val future = Future.value("Hello")
future.onSuccess { result =>
  println(result)
}

RxJava:

Observable<String> observable = Observable.just("Hello");
observable.subscribe(result -> {
    System.out.println(result);
});

Key Differences

  • util is primarily focused on Scala, while RxJava is Java-centric
  • RxJava offers a wider range of operators and transformations
  • util is more lightweight and may be easier to integrate into existing Twitter-based projects

Use Cases

  • util: Best for Twitter-specific projects or Scala-based applications
  • RxJava: Ideal for complex reactive programming needs across various Java platforms

Community and Maintenance

  • util: Maintained by Twitter, with a focus on their ecosystem
  • RxJava: Part of the larger ReactiveX project, with broader community involvement

Spring Framework

Pros of Spring Framework

  • Comprehensive ecosystem with extensive documentation and community support
  • Modular architecture allowing developers to use only needed components
  • Robust dependency injection and aspect-oriented programming features

Cons of Spring Framework

  • Steeper learning curve due to its extensive feature set
  • Can be considered "heavyweight" for smaller projects
  • Configuration can be complex, especially for beginners

Code Comparison

Spring Framework:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Util:

object HelloWorld extends App {
  val server = Http.serve(":8080", Service.mk { req =>
    Response(content = Buf.Utf8("Hello, World!"))
  })
}

Spring Framework offers a more declarative approach with annotations, while Util uses a more functional style. Spring's example is more concise, but Util's code provides more explicit control over the server setup.

Spring Framework is a comprehensive Java application framework, whereas Util is a collection of Scala utilities. Spring Framework is better suited for large-scale enterprise applications, while Util is more focused on providing specific utilities for Scala developers, particularly those working with Twitter's stack.

49,988

Google core libraries for Java

Pros of Guava

  • More comprehensive library with a wider range of utilities
  • Better documentation and more extensive community support
  • Actively maintained with frequent updates and improvements

Cons of Guava

  • Larger dependency size, which may impact project size and build times
  • Steeper learning curve due to its extensive API
  • Some features may be redundant with newer Java versions

Code Comparison

Guava:

List<String> list = Lists.newArrayList("a", "b", "c");
Multimap<String, Integer> multimap = ArrayListMultimap.create();
Optional<String> optional = Optional.of("value");

Util:

val list = Seq("a", "b", "c")
val multimap = new HashMap[String, Seq[Int]]()
val optional = Option("value")

Summary

Guava offers a more extensive set of utilities and better documentation, making it suitable for larger projects. Util, being more lightweight, may be preferable for smaller projects or those already using Twitter's stack. Guava's Java-based approach contrasts with Util's Scala focus, which may influence the choice depending on the project's primary language.

Apache Commons Lang

Pros of Commons Lang

  • Broader language support and more comprehensive utility classes
  • Extensive documentation and widespread community adoption
  • More frequent releases and updates

Cons of Commons Lang

  • Larger library size, potentially increasing application footprint
  • Less focus on concurrency and asynchronous programming
  • Some utility methods may be outdated due to Java language evolution

Code Comparison

Commons Lang:

String cleaned = StringUtils.trimToEmpty(input);
boolean isNumeric = NumberUtils.isCreatable(cleaned);
String reversed = StringUtils.reverse(cleaned);

Util:

val cleaned = input.trim
val isNumeric = Try(cleaned.toDouble).isSuccess
val reversed = cleaned.reverse

Key Differences

  • Commons Lang is Java-focused, while Util is primarily for Scala
  • Util includes Twitter-specific utilities and concurrency tools
  • Commons Lang offers more general-purpose string and number manipulation

Use Cases

  • Commons Lang: General Java development, string manipulation, reflection utilities
  • Util: Scala projects, Twitter-related development, concurrent programming

Community and Support

  • Commons Lang: Large Apache community, extensive documentation
  • Util: Smaller but active Twitter developer community, less comprehensive docs

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Twitter Util

Build Status Project status Gitter Maven Central

A bunch of idiomatic, small, general purpose tools.

See the Scaladoc here or check out the user guide.

Status

This project is used in production at Twitter (and many other organizations), and is being actively developed and maintained.

Releases

Releases are done on an approximately monthly schedule. While semver is not followed, the changelogs are detailed and include sections on public API breaks and changes in runtime behavior.

Contributing

We feel that a welcoming community is important and we ask that you follow Twitter's Open Source Code of Conduct in all interactions with the community.

The release branch of this repository contains the latest stable release of Util, and weekly snapshots are published to the develop branch. In general pull requests should be submitted against develop. See CONTRIBUTING.md for more details about how to contribute.

Using in your project

An example SBT dependency string for the util-core library would look like this:

val utilCore = "com.twitter" %% "util-core" % "24.5.0"

Units

Time

import com.twitter.conversions.DurationOps._

val duration1 = 1.second
val duration2 = 2.minutes
duration1.inMillis // => 1000L

Space

import com.twitter.conversions.StorageUnitOps._
val amount = 8.megabytes
amount.inBytes // => 8388608L
amount.inKilobytes // => 8192L

Futures

A Non-actor re-implementation of Scala Futures.

import com.twitter.conversions.DurationOps._
import com.twitter.util.{Await, Future, Promise}

val f = new Promise[Int]
val g = f.map { result => result + 1 }
f.setValue(1)
Await.result(g, 1.second) // => this blocks for the futures result (and eventually returns 2)

// Another option:
g.onSuccess { result =>
  println(result) // => prints "2"
}

// Using for expressions:
val xFuture = Future(1)
val yFuture = Future(2)

for {
  x <- xFuture
  y <- yFuture
} {
  println(x + y) // => prints "3"
}

Future interrupts

Method raise on Future (def raise(cause: Throwable)) raises the interrupt described by cause to the producer of this Future. Interrupt handlers are installed on a Promise using setInterruptHandler, which takes a partial function:

val p = new Promise[T]
p.setInterruptHandler {
  case exc: MyException =>
    // deal with interrupt..
}

Interrupts differ in semantics from cancellation in important ways: there can only be one interrupt handler per promise, and interrupts are only delivered if the promise is not yet complete.

Object Pool

The pool order is FIFO.

A pool of constants

import scala.collection.mutable
import com.twitter.util.{Await, SimplePool}

val queue = new mutable.Queue[Int] ++ List(1, 2, 3)
val pool = new SimplePool(queue)

// Note that the pool returns Futures, it doesn't block on exhaustion.
assert(Await.result(pool.reserve()) == 1)
pool.reserve().onSuccess { item =>
  println(item) // prints "2"
}

A pool of dynamically created objects

Here is a pool of even-number generators. It stores 4 numbers at a time:

import com.twitter.util.{Future, FactoryPool}

val pool = new FactoryPool[Int](4) {
  var count = 0
  def makeItem() = { count += 1; Future(count) }
  def isHealthy(i: Int) = i % 2 == 0
}

It checks the health when you successfully reserve an object (i.e., when the Future yields).

Hashing

util-hashing is a collection of hash functions and hashing distributors (eg. ketama).

To use one of the available hash functions:

import com.twitter.hashing.KeyHasher

KeyHasher.FNV1_32.hashKey("string".getBytes)

Available hash functions are:

FNV1_32
FNV1A_32
FNV1_64
FNV1A_64
KETAMA
CRC32_ITU
HSIEH

To use KetamaDistributor:

import com.twitter.hashing.{KetamaDistributor, KetamaNode, KeyHasher}

val nodes = List(KetamaNode("host:port", 1 /* weight */, "foo" /* handle */))
val distributor = new KetamaDistributor(nodes, 1 /* num reps */)
distributor.nodeForHash("abc".##) // => client

Time and Duration

Like arithmetic on doubles, Time and Duration arithmetic is now free of overflows. Instead, they overflow to Top and Bottom values, which are analogous to positive and negative infinity.

Since the resolution of Time.now has been reduced (and is also more expensive due to its use of system time), a new Stopwatch API has been introduced in order to calculate durations of time.

It's used simply:

import com.twitter.util.{Duration, Stopwatch}
val elapsed: () => Duration = Stopwatch.start()

which is read by applying elapsed:

val duration: Duration = elapsed()

License

Copyright 2010 Twitter, Inc.

Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0