= InteractionContext
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Provides the user and scenario specific environment for an xref:refguide:applib:index/services/iactn/Interaction.adoc[Interaction] .

== API

[source,java]
.InteractionContext.java
----
class InteractionContext {
  InteractionContext ofUserWithSystemDefaults(UserMemento user)     // <.>
  UserLocale getLocale()
  InteractionContext withTimeZoneIfAny(Optional<ZoneId> zoneIdIfAny)     // <.>
  UnaryOperator<InteractionContext> switchUser(UserMemento userMemento)     // <.>
  UnaryOperator<InteractionContext> switchClock(VirtualClock clock)     // <.>
  UnaryOperator<InteractionContext> switchLocale(UserLocale locale)     // <.>
  UnaryOperator<InteractionContext> switchTimeZone(ZoneId timeZone)     // <.>
  UnaryOperator<T> combine(UnaryOperator<T>... mappers)     // <.>
  UnaryOperator<T> combine(Stream<UnaryOperator<T>> mappers)     // <.>
  ZoneOffset getTimeZoneOffsetNow()     // <.>
}
----

<.> xref:#ofUserWithSystemDefaults_UserMemento[ofUserWithSystemDefaults(UserMemento)]
+
--
Creates a new xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] with the specified user and system defaults for clock, locale and time-zone.
--
<.> xref:#withTimeZoneIfAny_Optional[withTimeZoneIfAny(Optional)]
+
--
Only override if zoneIdIfAny is present, otherwise acts as identity operation.
--
<.> xref:#switchUser_UserMemento[switchUser(UserMemento)]
+
--
Convenience method for use with _org.apache.causeway.applib.services.sudo.SudoService_ , returning a _UnaryOperator_ that will act upon the provided xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] to return the same but with the specified xref:refguide:applib:index/services/user/UserMemento.adoc[UserMemento] .
--
<.> xref:#switchClock_VirtualClock[switchClock(VirtualClock)]
+
--
Convenience method for use with _org.apache.causeway.applib.services.sudo.SudoService_ , returning a _UnaryOperator_ that will act upon the provided xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] to return the same but with the specified xref:refguide:applib:index/clock/VirtualClock.adoc[VirtualClock] .
--
<.> xref:#switchLocale_UserLocale[switchLocale(UserLocale)]
+
--
Convenience method for use with _org.apache.causeway.applib.services.sudo.SudoService_ , returning a _UnaryOperator_ that will act upon the provided xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] to return the same but with the specified _Locale_ .
--
<.> xref:#switchTimeZone_ZoneId[switchTimeZone(ZoneId)]
+
--
Convenience method for use with _org.apache.causeway.applib.services.sudo.SudoService_ , returning a _UnaryOperator_ that will act upon the provided xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] to return the same but with the specified _ZoneId_ .
--
<.> xref:#combine_UnaryOperator[combine(UnaryOperator)]
+
--
Convenience method to combine _UnaryOperator_ s, for example as per _#switchUser(UserMemento)_ and _#switchTimeZone(ZoneId)_ .
--
<.> xref:#combine_Stream[combine(Stream)]
+
--
Convenience method to combine _UnaryOperator_ s, for example as per _#switchUser(UserMemento)_ and _#switchTimeZone(ZoneId)_ . credit: https://stackoverflow.com/a/51065029/56880
--
<.> xref:#getTimeZoneOffsetNow_[getTimeZoneOffsetNow()]
+
--
Returns the _ZoneOffset_ at the current time _Instant_ (at which _ZoneId_ rules apply to calculate the offset).
--

== Members

[#ofUserWithSystemDefaults_UserMemento]
=== ofUserWithSystemDefaults(UserMemento)

Creates a new xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] with the specified user and system defaults for clock, locale and time-zone.

[#withTimeZoneIfAny_Optional]
=== withTimeZoneIfAny(Optional)

Only override if zoneIdIfAny is present, otherwise acts as identity operation.

[#switchUser_UserMemento]
=== switchUser(UserMemento)

Convenience method for use with _org.apache.causeway.applib.services.sudo.SudoService_ , returning a _UnaryOperator_ that will act upon the provided xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] to return the same but with the specified xref:refguide:applib:index/services/user/UserMemento.adoc[UserMemento] .

[#switchClock_VirtualClock]
=== switchClock(VirtualClock)

Convenience method for use with _org.apache.causeway.applib.services.sudo.SudoService_ , returning a _UnaryOperator_ that will act upon the provided xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] to return the same but with the specified xref:refguide:applib:index/clock/VirtualClock.adoc[VirtualClock] .

[#switchLocale_UserLocale]
=== switchLocale(UserLocale)

Convenience method for use with _org.apache.causeway.applib.services.sudo.SudoService_ , returning a _UnaryOperator_ that will act upon the provided xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] to return the same but with the specified _Locale_ .

[#switchTimeZone_ZoneId]
=== switchTimeZone(ZoneId)

Convenience method for use with _org.apache.causeway.applib.services.sudo.SudoService_ , returning a _UnaryOperator_ that will act upon the provided xref:refguide:applib:index/services/iactnlayer/InteractionContext.adoc[InteractionContext] to return the same but with the specified _ZoneId_ .

[#combine_UnaryOperator]
=== combine(UnaryOperator)

Convenience method to combine _UnaryOperator_ s, for example as per _#switchUser(UserMemento)_ and _#switchTimeZone(ZoneId)_ .

NOTE: this implementation can result in heap pollution; better to use the _#combine(Stream) overload_ .

[#combine_Stream]
=== combine(Stream)

Convenience method to combine _UnaryOperator_ s, for example as per _#switchUser(UserMemento)_ and _#switchTimeZone(ZoneId)_ . credit: https://stackoverflow.com/a/51065029/56880

[#getTimeZoneOffsetNow_]
=== getTimeZoneOffsetNow()

Returns the _ZoneOffset_ at the current time _Instant_ (at which _ZoneId_ rules apply to calculate the offset).
