Java 17新特性前瞻:模式匹配、虚拟线程与Record类的实战应用

Max590
Max590 2026-02-05T12:13:04+08:00
0 0 2

引言

Java 17作为Java LTS(长期支持)版本,带来了众多重要的新特性和改进。从模式匹配语法糖到虚拟线程实现,再到Record类简化数据封装,这些新特性不仅提升了代码的可读性和开发效率,更为Java生态系统的现代化发展奠定了坚实基础。本文将深入探讨这些关键特性,通过实际代码示例和应用场景分析,帮助开发者更好地理解和应用Java 17的新功能。

Java 17核心新特性概览

模式匹配(Pattern Matching)

模式匹配是Java 17中最重要的新特性之一,它简化了类型检查和转换的复杂性。通过引入instanceof模式匹配和switch表达式的模式匹配,开发者可以编写更加简洁、安全的代码。

虚拟线程(Virtual Threads)

虚拟线程是Java 17中引入的轻量级线程实现,它大大降低了并发编程的复杂性和资源消耗。虚拟线程通过在JVM层面管理线程池,使得开发者能够轻松创建和使用大量线程而不会遇到性能瓶颈。

Record类(Record Classes)

Record类是Java 17中的一个重要改进,它简化了不可变数据类的创建过程。通过一个简单的声明,开发者可以快速定义包含数据、构造函数、getter方法、equals、hashCode和toString等方法的完整类。

模式匹配实战详解

instanceof模式匹配

传统的instanceof操作符需要先进行类型检查,然后进行强制转换,这在实际开发中经常导致代码冗余。Java 17引入了模式匹配语法,让这个过程变得更加简洁。

// Java 16及以前版本的写法
public String processObject(Object obj) {
    if (obj instanceof String) {
        String str = (String) obj;
        return str.toUpperCase();
    }
    return "Not a string";
}

// Java 17模式匹配写法
public String processObjectNew(Object obj) {
    if (obj instanceof String str) {
        return str.toUpperCase();
    }
    return "Not a string";
}

switch表达式模式匹配

在Java 17中,switch表达式支持更强大的模式匹配功能,包括类型匹配、常量匹配和null值处理。

// 复杂的switch表达式模式匹配示例
public String analyzeValue(Object obj) {
    return switch (obj) {
        case Integer i when i > 0 -> "Positive integer: " + i;
        case Integer i when i < 0 -> "Negative integer: " + i;
        case String s when s.length() > 10 -> "Long string: " + s;
        case String s -> "Short string: " + s;
        case Double d when d.isNaN() -> "Not a number";
        case Double d -> "Valid double: " + d;
        case null -> "Null value";
        default -> "Unknown type";
    };
}

// 复合类型的模式匹配
public Object processShape(Shape shape) {
    return switch (shape) {
        case Circle c -> c.area();
        case Rectangle r -> r.area();
        case Triangle t when t.getBase() > 10 -> t.area();
        case Triangle t -> t.area() * 2; // 默认情况
        default -> throw new IllegalArgumentException("Unknown shape");
    };
}

// 定义示例类
abstract class Shape {
    abstract double area();
}

class Circle extends Shape {
    private final double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    @Override
    double area() {
        return Math.PI * radius * radius;
    }
    
    public double getRadius() {
        return radius;
    }
}

class Rectangle extends Shape {
    private final double width, height;
    
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    @Override
    double area() {
        return width * height;
    }
    
    public double getWidth() {
        return width;
    }
    
    public double getHeight() {
        return height;
    }
}

模式匹配的最佳实践

// 实际业务场景中的模式匹配应用
public class OrderProcessor {
    
    // 处理不同类型的订单
    public String processOrder(Order order) {
        return switch (order) {
            case null -> "Invalid order";
            case OnlineOrder online when online.getPaymentMethod() == PaymentMethod.CREDIT_CARD -> 
                "Processing credit card payment for online order";
            case OnlineOrder online -> "Processing other payment method for online order";
            case OfflineOrder offline when offline.isExpressDelivery() -> 
                "Processing express delivery for offline order";
            case OfflineOrder offline -> "Processing standard delivery for offline order";
            default -> "Unknown order type";
        };
    }
    
    // 处理复杂的业务逻辑
    public double calculateDiscount(Object customer) {
        return switch (customer) {
            case GoldCustomer gold when gold.getPurchaseAmount() > 1000 -> 
                gold.getPurchaseAmount() * 0.15;
            case SilverCustomer silver when silver.getPurchaseAmount() > 500 -> 
                silver.getPurchaseAmount() * 0.10;
            case BronzeCustomer bronze when bronze.getPurchaseAmount() > 200 -> 
                bronze.getPurchaseAmount() * 0.05;
            case Customer c -> 0.0; // 普通客户无折扣
            case null -> 0.0; // 空值处理
            default -> throw new IllegalArgumentException("Unknown customer type");
        };
    }
}

// 定义相关类
abstract class Customer {
    private final String name;
    private final double purchaseAmount;
    
    public Customer(String name, double purchaseAmount) {
        this.name = name;
        this.purchaseAmount = purchaseAmount;
    }
    
    public String getName() {
        return name;
    }
    
    public double getPurchaseAmount() {
        return purchaseAmount;
    }
}

class GoldCustomer extends Customer {
    public GoldCustomer(String name, double purchaseAmount) {
        super(name, purchaseAmount);
    }
}

class SilverCustomer extends Customer {
    public SilverCustomer(String name, double purchaseAmount) {
        super(name, purchaseAmount);
    }
}

class BronzeCustomer extends Customer {
    public BronzeCustomer(String name, double purchaseAmount) {
        super(name, purchaseAmount);
    }
}

class Order {
    // 订单基础类
}

class OnlineOrder extends Order {
    private final PaymentMethod paymentMethod;
    
    public OnlineOrder(PaymentMethod paymentMethod) {
        this.paymentMethod = paymentMethod;
    }
    
    public PaymentMethod getPaymentMethod() {
        return paymentMethod;
    }
}

class OfflineOrder extends Order {
    private final boolean expressDelivery;
    
    public OfflineOrder(boolean expressDelivery) {
        this.expressDelivery = expressDelivery;
    }
    
    public boolean isExpressDelivery() {
        return expressDelivery;
    }
}

enum PaymentMethod {
    CREDIT_CARD, DEBIT_CARD, PAYPAL, BANK_TRANSFER
}

虚拟线程深度解析

虚拟线程的基本概念

虚拟线程(Virtual Threads)是Java 17中引入的一种轻量级线程实现。与传统的平台线程不同,虚拟线程由JVM在用户空间管理,大大减少了系统资源的消耗。

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class VirtualThreadExample {
    
    // 传统平台线程的使用方式
    public void traditionalThreadExample() {
        ExecutorService executor = Executors.newFixedThreadPool(1000);
        
        for (int i = 0; i < 1000; i++) {
            final int taskId = i;
            executor.submit(() -> {
                System.out.println("Task " + taskId + " executed by thread: " + 
                    Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
        }
        
        executor.shutdown();
    }
    
    // 虚拟线程的使用方式
    public void virtualThreadExample() {
        // 使用虚拟线程执行任务
        for (int i = 0; i < 1000; i++) {
            final int taskId = i;
            Thread.ofVirtual()
                .name("VirtualTask-" + taskId)
                .start(() -> {
                    System.out.println("Task " + taskId + " executed by virtual thread: " + 
                        Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                });
        }
    }
    
    // 虚拟线程与CompletableFuture结合使用
    public void virtualThreadWithFuture() {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("Processing in thread: " + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return "Result from virtual thread";
        }, Thread.ofVirtual().factory());
        
        future.thenAccept(result -> 
            System.out.println("Received result: " + result + 
                " on thread: " + Thread.currentThread().getName()));
    }
}

虚拟线程的性能优势

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class VirtualThreadPerformance {
    
    private static final int THREAD_COUNT = 10000;
    private static final AtomicInteger completedTasks = new AtomicInteger(0);
    
    // 性能对比测试
    public void performanceComparison() throws InterruptedException {
        long startTime, endTime;
        
        // 测试传统线程池性能
        startTime = System.currentTimeMillis();
        traditionalThreadPoolTest();
        endTime = System.currentTimeMillis();
        System.out.println("Traditional thread pool time: " + (endTime - startTime) + "ms");
        
        // 测试虚拟线程性能
        startTime = System.currentTimeMillis();
        virtualThreadTest();
        endTime = System.currentTimeMillis();
        System.out.println("Virtual threads time: " + (endTime - startTime) + "ms");
    }
    
    private void traditionalThreadPoolTest() throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(100);
        
        for (int i = 0; i < THREAD_COUNT; i++) {
            final int taskId = i;
            executor.submit(() -> {
                try {
                    Thread.sleep(10);
                    completedTasks.incrementAndGet();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
        }
        
        executor.shutdown();
        while (!executor.isTerminated()) {
            Thread.sleep(100);
        }
    }
    
    private void virtualThreadTest() throws InterruptedException {
        for (int i = 0; i < THREAD_COUNT; i++) {
            final int taskId = i;
            Thread.ofVirtual()
                .start(() -> {
                    try {
                        Thread.sleep(10);
                        completedTasks.incrementAndGet();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                });
        }
        
        // 等待所有任务完成
        while (completedTasks.get() < THREAD_COUNT) {
            Thread.sleep(10);
        }
    }
    
    // 异步编程中的虚拟线程应用
    public CompletableFuture<String> asyncProcessData(String data) {
        return CompletableFuture.supplyAsync(() -> {
            // 模拟数据处理
            try {
                Thread.sleep(500);
                return "Processed: " + data;
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            }
        }, Thread.ofVirtual().factory());
    }
    
    // 批量处理任务
    public CompletableFuture<Void> batchProcess(List<String> dataList) {
        List<CompletableFuture<String>> futures = dataList.stream()
            .map(this::asyncProcessData)
            .collect(Collectors.toList());
            
        return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
            .thenApply(v -> {
                futures.forEach(f -> System.out.println(f.join()));
                return null;
            });
    }
}

虚拟线程的实际应用场景

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;

public class VirtualThreadApplications {
    
    // Web服务中的异步处理
    public class AsyncWebService {
        private static final int MAX_CONCURRENT_REQUESTS = 1000;
        
        public CompletableFuture<String> handleRequest(String request) {
            return CompletableFuture.supplyAsync(() -> {
                // 模拟网络请求处理
                try {
                    Thread.sleep(ThreadLocalRandom.current().nextInt(100, 500));
                    return "Response for: " + request;
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException(e);
                }
            }, Thread.ofVirtual().factory());
        }
        
        // 批量处理请求
        public CompletableFuture<Void> processBatch(List<String> requests) {
            List<CompletableFuture<String>> futures = requests.stream()
                .map(this::handleRequest)
                .collect(Collectors.toList());
                
            return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
                .thenApply(v -> {
                    futures.forEach(f -> System.out.println(f.join()));
                    return null;
                });
        }
    }
    
    // 数据库操作中的虚拟线程
    public class DatabaseService {
        private static final int BATCH_SIZE = 100;
        
        public CompletableFuture<List<String>> processBatchDatabaseOperations(List<Record> records) {
            List<CompletableFuture<String>> futures = new ArrayList<>();
            
            for (int i = 0; i < records.size(); i += BATCH_SIZE) {
                int endIndex = Math.min(i + BATCH_SIZE, records.size());
                List<Record> batch = records.subList(i, endIndex);
                
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                    // 模拟数据库批量操作
                    try {
                        Thread.sleep(100 * (batch.size() / 10));
                        return "Processed " + batch.size() + " records";
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        throw new RuntimeException(e);
                    }
                }, Thread.ofVirtual().factory());
                
                futures.add(future);
            }
            
            return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
                .thenApply(v -> futures.stream()
                    .map(CompletableFuture::join)
                    .collect(Collectors.toList()));
        }
    }
    
    // 文件处理中的并发操作
    public class FileProcessingService {
        public CompletableFuture<Void> processFiles(List<String> filePaths) {
            List<CompletableFuture<Void>> futures = filePaths.stream()
                .map(filePath -> CompletableFuture.runAsync(() -> {
                    try {
                        // 模拟文件读取和处理
                        Thread.sleep(ThreadLocalRandom.current().nextInt(100, 1000));
                        System.out.println("Processed file: " + filePath);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }, Thread.ofVirtual().factory()))
                .collect(Collectors.toList());
                
            return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
        }
    }
}

Record类的深度应用

Record类基础语法与使用

Record类是Java 17中引入的语法糖,它简化了不可变数据类的创建过程。通过一个简单的声明,可以自动生成构造函数、getter方法、equals、hashCode和toString等方法。

// 基础Record类定义
public record Person(String name, int age, String email) {
    // 可以添加自定义方法
    public String getDisplayName() {
        return name + " (" + age + ")";
    }
    
    // 验证构造参数
    public Person {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative");
        }
        if (name == null || name.trim().isEmpty()) {
            throw new IllegalArgumentException("Name cannot be null or empty");
        }
    }
}

// 复杂的Record类示例
public record Employee(String firstName, String lastName, int employeeId, 
                      Department department, List<String> skills) {
    
    public Employee {
        if (employeeId <= 0) {
            throw new IllegalArgumentException("Employee ID must be positive");
        }
        if (department == null) {
            throw new IllegalArgumentException("Department cannot be null");
        }
        if (skills == null) {
            throw new IllegalArgumentException("Skills cannot be null");
        }
    }
    
    public String getFullName() {
        return firstName + " " + lastName;
    }
    
    public boolean hasSkill(String skill) {
        return skills.contains(skill);
    }
}

// 嵌套Record类
public record Company(String name, Address address, List<Employee> employees) {
    
    public Company {
        if (name == null || name.trim().isEmpty()) {
            throw new IllegalArgumentException("Company name cannot be null or empty");
        }
        if (address == null) {
            throw new IllegalArgumentException("Address cannot be null");
        }
        if (employees == null) {
            throw new IllegalArgumentException("Employees list cannot be null");
        }
    }
    
    public int getTotalEmployeeCount() {
        return employees.size();
    }
}

public record Address(String street, String city, String zipCode, String country) {
    
    public Address {
        if (street == null || street.trim().isEmpty()) {
            throw new IllegalArgumentException("Street cannot be null or empty");
        }
        if (city == null || city.trim().isEmpty()) {
            throw new IllegalArgumentException("City cannot be null or empty");
        }
        if (zipCode == null || zipCode.trim().isEmpty()) {
            throw new IllegalArgumentException("Zip code cannot be null or empty");
        }
        if (country == null || country.trim().isEmpty()) {
            throw new IllegalArgumentException("Country cannot be null or empty");
        }
    }
}

Record类的高级特性

// 可变Record类(通过private final字段)
public record MutablePerson(String name, int age) {
    
    // 由于Record是不可变的,我们可以通过工厂方法创建新实例
    public MutablePerson withAge(int newAge) {
        return new MutablePerson(this.name, newAge);
    }
    
    public MutablePerson withName(String newName) {
        return new MutablePerson(newName, this.age);
    }
}

// Record类中的方法重载和继承
public record Point(double x, double y) {
    
    // 静态工厂方法
    public static Point origin() {
        return new Point(0.0, 0.0);
    }
    
    // 带参数验证的构造器
    public Point {
        if (Double.isNaN(x) || Double.isInfinite(x)) {
            throw new IllegalArgumentException("X coordinate cannot be NaN or infinite");
        }
        if (Double.isNaN(y) || Double.isInfinite(y)) {
            throw new IllegalArgumentException("Y coordinate cannot be NaN or infinite");
        }
    }
    
    // 实用方法
    public double distance(Point other) {
        return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
    }
    
    public Point translate(double dx, double dy) {
        return new Point(this.x + dx, this.y + dy);
    }
}

// Record类的模式匹配
public class RecordPatternMatching {
    
    public String processPoint(Point point) {
        return switch (point) {
            case Point p when p.x() == 0 && p.y() == 0 -> "Origin";
            case Point p when p.x() == 0 -> "On Y-axis";
            case Point p when p.y() == 0 -> "On X-axis";
            case Point p -> "General point: (" + p.x() + ", " + p.y() + ")";
        };
    }
    
    // 在方法参数中使用模式匹配
    public double calculateArea(Object shape) {
        return switch (shape) {
            case Point p -> 0.0; // 点的面积为0
            case Rectangle r when r.width() > 0 && r.height() > 0 -> 
                r.width() * r.height();
            case Circle c when c.radius() > 0 -> Math.PI * c.radius() * c.radius();
            case null -> 0.0;
            default -> throw new IllegalArgumentException("Unknown shape");
        };
    }
}

// 复杂数据结构的Record实现
public record Order(String orderId, LocalDateTime orderDate, 
                   Customer customer, List<OrderItem> items, 
                   BigDecimal totalAmount) {
    
    public Order {
        if (orderId == null || orderId.trim().isEmpty()) {
            throw new IllegalArgumentException("Order ID cannot be null or empty");
        }
        if (orderDate == null) {
            throw new IllegalArgumentException("Order date cannot be null");
        }
        if (customer == null) {
            throw new IllegalArgumentException("Customer cannot be null");
        }
        if (items == null) {
            throw new IllegalArgumentException("Items list cannot be null");
        }
        if (totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("Total amount cannot be null or negative");
        }
    }
    
    public BigDecimal getTaxAmount() {
        return totalAmount.multiply(BigDecimal.valueOf(0.08)); // 8%税率
    }
    
    public BigDecimal getNetAmount() {
        return totalAmount.subtract(getTaxAmount());
    }
}

public record OrderItem(String productId, String productName, 
                       int quantity, BigDecimal unitPrice) {
    
    public OrderItem {
        if (productId == null || productId.trim().isEmpty()) {
            throw new IllegalArgumentException("Product ID cannot be null or empty");
        }
        if (productName == null || productName.trim().isEmpty()) {
            throw new IllegalArgumentException("Product name cannot be null or empty");
        }
        if (quantity <= 0) {
            throw new IllegalArgumentException("Quantity must be positive");
        }
        if (unitPrice == null || unitPrice.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("Unit price cannot be null or negative");
        }
    }
    
    public BigDecimal getTotalPrice() {
        return unitPrice.multiply(BigDecimal.valueOf(quantity));
    }
}

Record类在实际项目中的应用

// 配置管理中的Record类
public record DatabaseConfig(String url, String username, String password, 
                           int maxConnections, boolean sslEnabled) {
    
    public DatabaseConfig {
        if (url == null || url.trim().isEmpty()) {
            throw new IllegalArgumentException("Database URL cannot be null or empty");
        }
        if (username == null || username.trim().isEmpty()) {
            throw new IllegalArgumentException("Username cannot be null or empty");
        }
        if (maxConnections <= 0) {
            throw new IllegalArgumentException("Max connections must be positive");
        }
    }
    
    public String getConnectionString() {
        return url + "?user=" + username + "&ssl=" + sslEnabled;
    }
}

// API响应的Record类
public record ApiResponse<T>(boolean success, String message, T data, 
                           int statusCode, LocalDateTime timestamp) {
    
    public ApiResponse {
        if (message == null) {
            throw new IllegalArgumentException("Message cannot be null");
        }
        if (timestamp == null) {
            throw new IllegalArgumentException("Timestamp cannot be null");
        }
    }
    
    public static <T> ApiResponse<T> success(T data, String message) {
        return new ApiResponse<>(true, message, data, 200, LocalDateTime.now());
    }
    
    public static <T> ApiResponse<T> error(String message, int statusCode) {
        return new ApiResponse<>(false, message, null, statusCode, LocalDateTime.now());
    }
}

// 用户认证中的Record类
public record AuthenticationResult(boolean authenticated, String token, 
                                 User user, LocalDateTime expiryTime) {
    
    public AuthenticationResult {
        if (authenticated && token == null) {
            throw new IllegalArgumentException("Token cannot be null for successful authentication");
        }
        if (authenticated && user == null) {
            throw new IllegalArgumentException("User cannot be null for successful authentication");
        }
        if (expiryTime == null) {
            throw new IllegalArgumentException("Expiry time cannot be null");
        }
    }
    
    public boolean isTokenExpired() {
        return LocalDateTime.now().isAfter(expiryTime);
    }
    
    public static AuthenticationResult success(String token, User user) {
        return new AuthenticationResult(true, token, user, 
                                      LocalDateTime.now().plusHours(24));
    }
    
    public static AuthenticationResult failure(String message) {
        return new AuthenticationResult(false, null, null, LocalDateTime.now());
    }
}

// 业务实体类
public record User(String id, String username, String email, 
                  LocalDateTime createdAt, boolean isActive) {
    
    public User {
        if (id == null || id.trim().isEmpty()) {
            throw new IllegalArgumentException("User ID cannot be null or empty");
        }
        if (username == null || username.trim().isEmpty()) {
            throw new IllegalArgumentException("Username cannot be null or empty");
        }
        if (email == null || email.trim().isEmpty()) {
            throw new IllegalArgumentException("Email cannot be null or empty");
        }
        if (createdAt == null) {
            throw new IllegalArgumentException("Created at cannot be null");
        }
    }
    
    public boolean isAccountActive() {
        return isActive && createdAt.isAfter(LocalDateTime.now().minusYears(1));
    }
}

// 数据处理中的Record类
public record ProcessingResult(String taskId, List<String> results, 
                              long processingTime, boolean successful) {
    
    public ProcessingResult {
        if (taskId == null || taskId.trim().isEmpty()) {
            throw new IllegalArgumentException("Task ID cannot be null or empty");
        }
        if (results == null) {
            throw new IllegalArgumentException("Results cannot be null");
        }
        if (processingTime < 0) {
            throw new IllegalArgumentException("Processing time cannot be negative");
        }
    }
    
    public static ProcessingResult success(String taskId, List<String> results, 
                                         long processingTime) {
        return new ProcessingResult(taskId, results, processingTime, true);
    }
    
    public static ProcessingResult failure(String taskId, String errorMessage, 
                                         long processingTime) {
        return new ProcessingResult(taskId, Arrays.asList(errorMessage), 
                                  processingTime, false);
    }
}

综合应用案例

完整的业务系统示例

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

// 综合应用:电商平台订单处理系统
public class ECommercePlatform {
    
    // 使用Record类定义核心数据结构
    public record Product(String id, String name, BigDecimal price, 
                         int stockQuantity, String category) {
        public Product {
            if (id == null || id.trim().isEmpty()) {
                throw new IllegalArgumentException("Product ID cannot be null or empty");
            }
            if (name == null || name.trim().isEmpty()) {
                throw new IllegalArgumentException("Product name cannot be null or empty");
            }
            if (price == null || price.compareTo(BigDecimal.ZERO) < 0) {
                throw new IllegalArgumentException("Price cannot be null or negative");
            }
            if (stockQuantity < 0) {
                throw new IllegalArgumentException("Stock quantity cannot be negative");
            }
        }
    }
    
    public record CartItem(Product product, int quantity) {
        public CartItem {
            if (product == null) {
                throw new IllegalArgumentException("Product cannot be null");
            }
            if (quantity <= 0) {
                throw new IllegalArgumentException("Quantity must be positive");
            }
        }
        
        public BigDecimal getTotalPrice() {
            return product.price().multiply(BigDecimal.valueOf(quantity));
        }
    }
    
    public record Order(String orderId, LocalDateTime orderDate, 
                       Customer customer, List<CartItem> items, 
                       BigDecimal totalAmount, OrderStatus status) {
        public Order {
            if (orderId == null || orderId.trim().
相关推荐
广告位招租

相似文章

    评论 (0)

    0/2000