Results 1 to 8 of 8

Thread: Generic Cup(Of T)

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Generic Cup(Of T)

    Based on Shaggy's famous quote:
    Quote Originally Posted by Shaggy Hiker View Post
    You HAVE heard of a List (of T), which is probably the most common of the generic objects, but there are also the Queue(of T), Stack(of T), and Cup (of T), though that last one isn't found in the Generic namespace, but rather in the British namespace.
    Here is a fully functional Cup(Of T).

    Features:
    • Add content to your Cup(Of T).
    • Be careful not to overflow it though!
    • Has a Temperature property.
    • If the temperature is not too high you can take a drink or sip via the Drink and Sip methods.
    • You can also Stir your Cup(Of T).
    • If your Cup(Of T) is empty, you can fill it via the Refill method.
    • If it is too hot you can cool it by simply waiting or calling the Blow method a few times.
    • If your T has become too cold you can reheat it via the Reheat method, while specifying the time to reheat.
    • Watch out, if the temperature gets too high you will slowly evaporate your T!


    Source:
    vb.net Code:
    1. Namespace British
    2.  
    3.     Public Class Cup(Of T)
    4.  
    5.         Private _Random As Random
    6.         Private _Timer As Timer
    7.         Private _ReheatingTime As TimeSpan
    8.         Private _ReheatingStopwatch As Stopwatch
    9.         Private _IsReheating As Boolean
    10.  
    11.         Protected Contents As Stack(Of T)
    12.  
    13.         ''' <summary>
    14.         ''' Creates a new Cup(Of T) of the specified size.
    15.         ''' </summary>
    16.         ''' <param name="size">How much T your Cup(Of T) can hold without overflowing.</param>
    17.         Public Sub New(size As Integer)
    18.             If size <= 0 Then
    19.                 Throw New ArgumentOutOfRangeException("size", "Such a small cup does not exist!")
    20.             End If
    21.  
    22.             _Size = size
    23.             _Random = New Random()
    24.             Me.Contents = New Stack(Of T)()
    25.             Me.Temperature = 0
    26.  
    27.             _Timer = New Timer()
    28.             _Timer.Interval = 500
    29.             AddHandler _Timer.Tick, AddressOf Timer_Tick
    30.         End Sub
    31.  
    32.         ''' <summary>
    33.         ''' Creates a new Cup(Of T) of the specified size.
    34.         ''' </summary>
    35.         ''' <param name="size">How much T your Cup(Of T) can hold without overflowing.</param>
    36.         ''' <param name="contents">The initial contents of your Cup(Of T)</param>
    37.         Public Sub New(size As Integer, contents As IEnumerable(Of T))
    38.             Me.New(size)
    39.             For Each content As T In contents
    40.                 Me.Push(content)
    41.             Next
    42.             Me.Temperature = 100
    43.         End Sub
    44.  
    45. #Region " Properties "
    46.  
    47.         Private _Temperature As Integer
    48.         ''' <summary>
    49.         ''' Gets the current temperature of your Cup(Of T). Be careful when drinking hot fluids!
    50.         ''' </summary>
    51.         ''' <returns>The current temperature of your Cup(Of T).</returns>
    52.         Public Property Temperature() As Integer
    53.             Get
    54.                 Return _Temperature
    55.             End Get
    56.             Private Set(ByVal value As Integer)
    57.                 If _Temperature <> value Then
    58.                     _Temperature = value
    59.                     Me.OnTemperatureChanged(EventArgs.Empty)
    60.                 End If
    61.             End Set
    62.         End Property
    63.  
    64.         Private _Size As Integer
    65.         ''' <summary>
    66.         ''' Gets the size of your cup. Be careful not to overflow it!
    67.         ''' </summary>
    68.         ''' <returns>The size of your cup.</returns>
    69.         Public ReadOnly Property Size() As Integer
    70.             Get
    71.                 Return _Size
    72.             End Get
    73.         End Property
    74.  
    75. #End Region
    76.  
    77. #Region " Methods "
    78.  
    79.         Private Sub Push(content As T)
    80.             Me.Contents.Push(content)
    81.             If Me.Contents.Count > Me.Size Then
    82.                 Throw New OverflowException("Your cup is too full and has overflown. Look at the mess you made!")
    83.             End If
    84.         End Sub
    85.  
    86.         Private Sub CanDrink()
    87.             If Me.Contents.Count = 0 Then
    88.                 Throw New EmptyCupException("You cannot drink from an empty cup. Try calling Refill() first.")
    89.             End If
    90.             If Me.Temperature <= 0 Then
    91.                 Throw New TemperatureException("Your T has frozen. Try calling Reheat() first.")
    92.             End If
    93.             If Me.Temperature >= 40 Then
    94.                 Throw New TemperatureException("You burned your tongue. Try waiting some more or calling Blow() first.")
    95.             End If
    96.         End Sub
    97.  
    98.         ''' <summary>
    99.         ''' If you can currently drink the T, you will take a small sip.
    100.         ''' </summary>
    101.         Public Sub Sip()
    102.             Me.CanDrink()
    103.             Me.Contents.Pop()
    104.         End Sub
    105.  
    106.         ''' <summary>
    107.         ''' If you can currently drink the T, you will drink about a quarter of the T.
    108.         ''' </summary>
    109.         Public Sub Drink()
    110.             Me.CanDrink()
    111.  
    112.             Dim amount As Integer = _Random.Next(CInt(Me.Contents.Count * (1 / 8)), CInt(Me.Contents.Count * (3 / 8)))
    113.             For i As Integer = 0 To amount - 1
    114.                 Me.Contents.Pop()
    115.             Next
    116.         End Sub
    117.  
    118.         ''' <summary>
    119.         ''' Stir around the contents of the T.
    120.         ''' </summary>
    121.         Public Sub Stir()
    122.             Dim oldContents = Me.Contents.ToList()
    123.             Dim newContents = From c As T In oldContents Order By _Random.NextDouble() Select c
    124.  
    125.             Me.Contents.Clear()
    126.             For Each content In newContents
    127.                 Me.Push(content)
    128.             Next
    129.         End Sub
    130.  
    131.         ''' <summary>
    132.         ''' Add some extra T to your cup. Note that the temperature will change!
    133.         ''' </summary>
    134.         Public Sub Refill(contents As IEnumerable(Of T))
    135.             Dim amount = Me.Contents.Count
    136.             For Each content As T In contents
    137.                 Me.Push(content)
    138.             Next
    139.  
    140.             Dim ratio = amount / Me.Contents.Count
    141.             Me.Temperature = CInt(Me.Temperature * ratio + 100 * (ratio - 1))
    142.         End Sub
    143.  
    144.         ''' <summary>
    145.         ''' Reheats your T for the specified time.
    146.         ''' </summary>
    147.         ''' <param name="time">The time to reheat your T for. Be careful not to make it too hot!</param>
    148.         Public Sub Reheat(time As TimeSpan)
    149.             _IsReheating = True
    150.             _ReheatingTime = time
    151.             _ReheatingStopwatch = New Stopwatch
    152.             _ReheatingStopwatch.Start()
    153.         End Sub
    154.  
    155.         ''' <summary>
    156.         ''' Blows in your Cup(Of T) to cool it down.
    157.         ''' </summary>
    158.         Public Sub Blow()
    159.             Me.Temperature -= 10
    160.         End Sub
    161.  
    162.         ''' <summary>
    163.         ''' If the temperature is too high some T will evaporate!
    164.         ''' </summary>
    165.         Private Sub Evaporate()
    166.             For i As Integer = 0 To _Random.Next(0, 5)
    167.                 If Me.Contents.Count > 0 Then Me.Contents.Pop()
    168.             Next
    169.         End Sub
    170.  
    171.         Private Sub Timer_Tick(sender As Object, e As EventArgs)
    172.             If _IsReheating Then
    173.                 If _ReheatingStopwatch.Elapsed >= _ReheatingTime Then
    174.                     _ReheatingStopwatch.Stop()
    175.                     _IsReheating = False
    176.                 End If
    177.  
    178.                 Me.Temperature += _Random.Next(10, 20)
    179.             Else
    180.                 Me.Temperature -= _Random.Next(0, 5)
    181.             End If
    182.  
    183.             If Me.Temperature >= 100 Then
    184.                 Me.Evaporate()
    185.             End If
    186.         End Sub
    187.  
    188. #End Region
    189.  
    190. #Region " Events "
    191.  
    192.         Public Event TemperatureChanged As EventHandler
    193.  
    194.         Protected Overridable Sub OnTemperatureChanged(e As EventArgs)
    195.             RaiseEvent TemperatureChanged(Me, e)
    196.         End Sub
    197.  
    198. #End Region
    199.  
    200. #Region " Nested classes "
    201.  
    202.         Public Class OverflowException
    203.             Inherits System.Exception
    204.  
    205.             Public Sub New(msg As String)
    206.                 MyBase.New(msg)
    207.             End Sub
    208.         End Class
    209.  
    210.         Public Class EmptyCupException
    211.             Inherits System.Exception
    212.  
    213.             Public Sub New(msg As String)
    214.                 MyBase.New(msg)
    215.             End Sub
    216.         End Class
    217.  
    218.         Public Class TemperatureException
    219.             Inherits System.Exception
    220.  
    221.             Public Sub New(msg As String)
    222.                 MyBase.New(msg)
    223.             End Sub
    224.         End Class
    225.  
    226. #End Region
    227.  
    228.     End Class
    229.  
    230. End Namespace


    Yeah, I needed some distraction after spending a sunday morning studying semiconductor physics... Don't blame me

    If you find some actual use for this, let me know
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width