001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.http.part; 014 015import static java.time.format.DateTimeFormatter.*; 016import static java.time.temporal.ChronoUnit.*; 017import static org.apache.juneau.common.internal.StringUtils.*; 018import static org.apache.juneau.internal.CollectionUtils.*; 019 020import java.time.*; 021import java.time.format.*; 022import java.util.*; 023import java.util.function.*; 024 025import org.apache.http.*; 026import org.apache.juneau.assertions.*; 027 028/** 029 * A {@link NameValuePair} that consist of a single HTTP-date. 030 * 031 * <h5 class='section'>See Also:</h5><ul> 032 * <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a> 033 * </ul> 034 */ 035public class BasicDatePart extends BasicPart { 036 037 //----------------------------------------------------------------------------------------------------------------- 038 // Static 039 //----------------------------------------------------------------------------------------------------------------- 040 041 /** 042 * Static creator. 043 * 044 * @param name The part name. 045 * @param value The part value. 046 * @return A new {@link BasicDatePart} object, or <jk>null</jk> if the name or value is <jk>null</jk>. 047 */ 048 public static BasicDatePart of(String name, ZonedDateTime value) { 049 if (isEmpty(name) || value == null) 050 return null; 051 return new BasicDatePart(name, value); 052 } 053 054 /** 055 * Static creator with delayed value. 056 * 057 * <p> 058 * Part value is re-evaluated on each call to {@link NameValuePair#getValue()}. 059 * 060 * @param name The part name. 061 * @param value The part value supplier. 062 * @return A new {@link BasicDatePart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>. 063 */ 064 public static BasicDatePart of(String name, Supplier<ZonedDateTime> value) { 065 if (isEmpty(name) || value == null) 066 return null; 067 return new BasicDatePart(name, value); 068 } 069 070 //----------------------------------------------------------------------------------------------------------------- 071 // Instance 072 //----------------------------------------------------------------------------------------------------------------- 073 074 private final ZonedDateTime value; 075 private final Supplier<ZonedDateTime> supplier; 076 077 /** 078 * Constructor. 079 * 080 * @param name The part name. Must not be <jk>null</jk>. 081 * @param value The part value. Can be <jk>null</jk>. 082 */ 083 public BasicDatePart(String name, ZonedDateTime value) { 084 super(name, value); 085 this.value = value; 086 this.supplier = null; 087 } 088 089 /** 090 * Constructor. 091 * 092 * @param name The part name. Must not be <jk>null</jk>. 093 * @param value The part value supplier. Can be <jk>null</jk> or supply <jk>null</jk>. 094 */ 095 public BasicDatePart(String name, Supplier<ZonedDateTime> value) { 096 super(name, value); 097 this.value = null; 098 this.supplier = value; 099 } 100 101 /** 102 * Constructor. 103 * 104 * <p> 105 * <jk>null</jk> and empty values are treated as <jk>null</jk>. 106 * Otherwise parses using {@link DateTimeFormatter#ISO_DATE_TIME}. 107 * 108 * @param name The part name. Must not be <jk>null</jk>. 109 * @param value The part value. Can be <jk>null</jk>. 110 */ 111 public BasicDatePart(String name, String value) { 112 super(name, value); 113 this.value = isEmpty(value) ? null : ZonedDateTime.from(ISO_DATE_TIME.parse(value)).truncatedTo(SECONDS); 114 this.supplier = null; 115 } 116 117 @Override /* Header */ 118 public String getValue() { 119 ZonedDateTime v = value(); 120 return v == null ? null : ISO_DATE_TIME.format(v); 121 } 122 123 /** 124 * Returns The part value as a {@link ZonedDateTime} wrapped in an {@link Optional}. 125 * 126 * @return The part value as a {@link ZonedDateTime} wrapped in an {@link Optional}. Never <jk>null</jk>. 127 */ 128 public Optional<ZonedDateTime> asZonedDateTime() { 129 return optional(toZonedDateTime()); 130 } 131 132 /** 133 * Returns The part value as a {@link ZonedDateTime}. 134 * 135 * @return The part value as a {@link ZonedDateTime}, or <jk>null</jk> if the value <jk>null</jk>. 136 */ 137 public ZonedDateTime toZonedDateTime() { 138 return value(); 139 } 140 141 /** 142 * Provides the ability to perform fluent-style assertions on this part. 143 * 144 * @return A new fluent assertion object. 145 * @throws AssertionError If assertion failed. 146 */ 147 public FluentZonedDateTimeAssertion<BasicDatePart> assertZonedDateTime() { 148 return new FluentZonedDateTimeAssertion<>(value(), this); 149 } 150 151 /** 152 * Return the value if present, otherwise return <c>other</c>. 153 * 154 * <p> 155 * This is a shortened form for calling <c>asZonedDateTime().orElse(<jv>other</jv>)</c>. 156 * 157 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 158 * @return The value, if present, otherwise <c>other</c>. 159 */ 160 public ZonedDateTime orElse(ZonedDateTime other) { 161 ZonedDateTime x = value(); 162 return x != null ? x : other; 163 } 164 165 private ZonedDateTime value() { 166 if (supplier != null) 167 return supplier.get(); 168 return value; 169 } 170}