A foundational Java library providing essential utilities and components for the CodeLibs project ecosystem. Built with Java 21 and optimized for modern Java development patterns including pattern matching, switch expressions, and sequenced collections.
- Bean Manipulation (
org.codelibs.core.beans
) - JavaBeans metadata handling, property access, and object conversion with comprehensiveBeanDesc
system - Type Conversion (
org.codelibs.core.convert
) - Comprehensive utilities for converting between Java types with null-safe operations and support for all primitive types - Collections (
org.codelibs.core.collection
) - Enhanced collection utilities, array operations, and specialized map/set implementations including LRU caches and case-insensitive collections - I/O Operations (
org.codelibs.core.io
) - File handling, resource management, stream utilities, and traversal utilities for efficient resource processing - Reflection (
org.codelibs.core.lang
) - Class loading, method/field access, type introspection utilities, and generics support - Exception Handling (
org.codelibs.core.exception
) - Runtime exception wrappers for common checked exceptions with consistent error handling
- Pattern Matching - Leverages modern Java pattern matching for efficient type checking and conversion
- Switch Expressions - Optimized implementations using switch expressions for better performance
- Sequenced Collections - Full support for Java 21 sequenced collections API with dedicated utility methods
- Performance Focused - Optimized implementations for better runtime performance with reduced memory allocation
- Type Safe - Comprehensive use of generics and modern Java type system features
- Text Processing (
org.codelibs.core.text
) - JSON utilities, tokenization, decimal formatting, and text manipulation - Logging Abstraction (
org.codelibs.core.log
) - Flexible logging system supporting JCL (Jakarta Commons Logging) and JUL (Java Util Logging) - Concurrent Utilities (
org.codelibs.core.concurrent
) - Thread-safe collections and concurrency helpers using modern concurrent APIs - Crypto & Security (
org.codelibs.core.crypto
,org.codelibs.core.security
) - Basic cryptographic utilities, message digest operations, and secure random generation - XML Processing (
org.codelibs.core.xml
) - XML DOM utilities, SAX parser helpers, and schema validation support - SQL Utilities (
org.codelibs.core.sql
) - JDBC helper methods for result sets, prepared statements, and connection management - Network & I/O (
org.codelibs.core.net
,org.codelibs.core.nio
) - URL utilities, UUID generation, MIME type detection, and NIO channel operations
- Java 21 or higher (Required for modern language features)
- Maven 3.6+ or Gradle 7+ for build management
- Optional: SLF4J or Commons Logging for logging support
<dependency>
<groupId>org.codelibs</groupId>
<artifactId>corelib</artifactId>
<version>0.7.0</version>
</dependency>
implementation 'org.codelibs:corelib:0.7.0'
import org.codelibs.core.beans.*;
import org.codelibs.core.beans.factory.BeanDescFactory;
import org.codelibs.core.beans.util.BeanUtil;
import org.codelibs.core.beans.util.CopyOptions;
// Bean metadata introspection
BeanDesc beanDesc = BeanDescFactory.getBeanDesc(MyBean.class);
PropertyDesc nameProperty = beanDesc.getPropertyDesc("name");
nameProperty.setValue(bean, "John Doe");
// Bean copying with flexible options
BeanUtil.copyBeanToBean(source, destination);
BeanUtil.copyBeanToBean(source, destination, options ->
options.exclude("password", "internalId"));
// Convert between beans and maps
Map<String, Object> map = BeanUtil.copyBeanToNewMap(bean);
MyBean newBean = BeanUtil.copyMapToNewBean(map, MyBean.class);
import org.codelibs.core.convert.*;
// Safe type conversions with null handling
Integer value = IntegerConversionUtil.toInteger("123"); // Returns 123
Integer nullValue = IntegerConversionUtil.toInteger(null); // Returns null
Boolean flag = BooleanConversionUtil.toBoolean("true"); // Returns true
Date date = DateConversionUtil.toDate("2023-12-25", "yyyy-MM-dd");
// Primitive conversions with default values
int primitiveInt = IntegerConversionUtil.toPrimitiveInt(value, "0"); // Default to 0 if null
import org.codelibs.core.collection.CollectionsUtil;
import java.util.SequencedCollection;
// Enhanced collection creation
List<String> list = CollectionsUtil.newArrayList();
Map<String, Object> map = CollectionsUtil.newLinkedHashMap();
Set<String> caseInsensitiveSet = new CaseInsensitiveSet<>();
// Java 21 Sequenced Collections support
SequencedCollection<String> sequenced = CollectionsUtil.newLinkedHashSet();
String first = CollectionsUtil.getFirst(sequenced);
String last = CollectionsUtil.getLast(sequenced);
SequencedCollection<String> reversed = CollectionsUtil.reversed(sequenced);
// Specialized collections
LruHashMap<String, Object> lruCache = new LruHashMap<>(100); // LRU cache with max 100 entries
CaseInsensitiveMap<String> configMap = new CaseInsensitiveMap<>();
import org.codelibs.core.io.*;
// Resource loading and management
URL resource = ResourceUtil.getResource("config.properties");
Properties props = PropertiesUtil.load(resource);
// File operations with proper resource handling
try (InputStream input = ResourceUtil.getResourceAsStream("data.txt")) {
String content = InputStreamUtil.getUTF8String(input);
}
// Resource traversal for processing multiple files
ResourceTraversalUtil.forEach("META-INF", (resource, is) -> {
// Process each resource in the META-INF directory
System.out.println("Processing: " + resource);
});
import org.codelibs.core.text.*;
// JSON utilities with proper escaping
String escaped = JsonUtil.escape("Hello \"World\" with special chars");
String unescaped = JsonUtil.unescape(escaped);
// Text tokenization
Tokenizer tokenizer = new Tokenizer("field1,field2,field3", ",");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
// Process each token
}
// Decimal formatting
DecimalFormat format = DecimalFormatUtil.getDecimalFormat("###,###.00");
import org.codelibs.core.exception.*;
// Runtime exception wrappers eliminate try-catch boilerplate
try {
// Code that might throw checked exceptions
return ClassUtil.newInstance(className); // Wraps checked exceptions automatically
} catch (ClassNotFoundRuntimeException e) {
// Handle the wrapped exception
logger.error("Class not found: " + className, e);
}
CoreLib follows a utility-class pattern where most functionality is exposed through static methods:
- Assertion-based validation - All methods validate inputs using
AssertionUtil.assertArgumentNotNull()
andAssertionUtil.assertArgumentNotEmpty()
- Exception wrapping - Checked exceptions are consistently wrapped in runtime exceptions (e.g.,
ClassNotFoundException
βClassNotFoundRuntimeException
) - Bean introspection - The comprehensive
BeanDesc
system provides metadata about JavaBeans, accessed throughBeanDescFactory.getBeanDesc(Class)
- Type safety - Extensive use of generics and null-safe operations throughout the API
- Resource management - Proper handling of resources with utilities like
CloseableUtil
andDisposableUtil
- Factory Pattern -
BeanDescFactory
for creating bean descriptors,ParameterizedClassDescFactory
for generic type handling - Builder Pattern -
CopyOptions
for configuring bean copying operations with fluent API - Adapter Pattern - Logging adapters (
JclLoggerAdapter
,JulLoggerAdapter
) for different logging frameworks - Template Method - Resource traversal utilities with customizable handlers
- Utility Classes - All core functionality exposed through static utility methods for easy access
- Caching - Bean descriptors and reflection metadata are cached for improved performance
- Lazy initialization - Resources and expensive operations are initialized only when needed
- Memory efficient - Specialized collections like
LruHashMap
andArrayMap
for memory-conscious applications - Java 21 features - Switch expressions and pattern matching for reduced overhead
# Clone the repository
git clone https://github.com/codelibs/corelib.git
cd corelib
# Compile the project
mvn clean compile
# Run all tests
mvn test
# Run specific test class
mvn test -Dtest=BeanUtilTest
# Run specific test method
mvn test -Dtest=BeanUtilTest#testCopyBeanToBean
# Format code according to project standards
mvn formatter:format
# Apply license headers to source files
mvn license:format
# Build JAR with all verifications
mvn clean package
# Generate test coverage report
mvn verify
# Coverage report available at: target/site/jacoco/index.html
corelib/
βββ src/main/java/org/codelibs/core/
β βββ beans/ # Bean manipulation and introspection
β βββ collection/ # Enhanced collection utilities
β βββ convert/ # Type conversion utilities
β βββ exception/ # Runtime exception wrappers
β βββ io/ # I/O and resource management
β βββ lang/ # Reflection and language utilities
β βββ log/ # Logging abstraction
β βββ text/ # Text processing utilities
β βββ xml/ # XML processing utilities
β βββ ... # Additional utility packages
βββ src/test/java/ # Comprehensive test suite
CoreLib 0.7.0 includes significant performance improvements through Java 21 optimizations:
- 5-15% faster type conversions with pattern matching and switch expressions
- Reduced memory allocation in collection operations and bean copying
- Improved reflection performance with cached descriptors and optimized field access
- Enhanced collection operations with Java 21 sequenced collections support
- Better concurrent performance using modern concurrent collection implementations
CoreLib supports multiple logging frameworks. Configure your preferred logger:
// Use with SLF4J (add slf4j-api dependency)
Logger logger = Logger.getLogger(MyClass.class);
// Use with Commons Logging (add commons-logging dependency)
Logger logger = Logger.getLogger(MyClass.class);
// Use with Java Util Logging (built-in)
Logger logger = Logger.getLogger(MyClass.class.getName());
// Configure bean copying behavior
CopyOptions options = new CopyOptions()
.exclude("password", "internalId") // Exclude specific fields
.includeNull(false) // Skip null values
.converter("dateField", new DateConverter("yyyy-MM-dd"));
BeanUtil.copyBeanToBean(source, dest, options);
We welcome contributions! Please see our contributing guidelines for details.
- Fork the repository on GitHub
- Create your feature branch:
git checkout -b feature/amazing-feature
- Follow the coding standards:
mvn formatter:format
- Add comprehensive tests for new functionality
- Commit your changes:
git commit -m 'Add amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Submit a Pull Request with detailed description
- Follow the project's Eclipse formatter configuration
- Add Apache License 2.0 headers to new files:
mvn license:format
- Maintain comprehensive JavaDoc documentation
- Write thorough unit tests with good coverage
- Follow existing naming conventions and patterns
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.